Javascript Jquery上下文菜单调用函数
我想调用上下文菜单中的函数。我试着做按钮,效果非常好。当我尝试在上下文菜单中放置时,我无法调用该函数。我使用这个库作为上下文菜单 我的桌子:Javascript Jquery上下文菜单调用函数,javascript,jquery,contextmenu,Javascript,Jquery,Contextmenu,我想调用上下文菜单中的函数。我试着做按钮,效果非常好。当我尝试在上下文菜单中放置时,我无法调用该函数。我使用这个库作为上下文菜单 我的桌子: <table id="ppmpsupplies" class="table table-bordered table-hover" cellspacing="0" width="100%"> <thead> <tr> <
<table id="ppmpsupplies" class="table table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>Code</th>
<th>General Description</th>
<th>Unit</th>
<th>Quantity</th>
<th>Estimated Budget</th>
<th>Mode of Procurement</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($items as $item){?>
<tr>
<td><?php echo $item->id;?></td>
<td><?php echo $item->description;?></td>
<td><?php echo $item->unit;?></td>
<td><?php echo $item->quantity;?></td>
<td><?php echo $item->budget;?></td>
<td><?php echo $item->mode;?></td>
</tr>
<?php }?>
</tbody>
<tfoot>
<td colspan="3"></td>
<td>Total</td>
<td></td>
</tfoot>
</table>
我的职能:
function edit_item(id) {
save_method = 'update';
$('#gcjform')[0].reset();
$.ajax({
url: "<?php echo site_url('ppmp/ajax_edit/')?>" + id,
type: "GET",
dataType: "JSON",
success: function(data) {
$('[name="id"]').val(data.id);
$('[name="description"]').val(data.description);
$('[name="unit"]').val(data.unit);
$('[name="quantity"]').val(data.quantity);
$('[name="budget"]').val(data.budget);
$('[name="mode"]').val(data.mode);
$('#gcjmodal').iziModal('open');
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error get data from ajax');
}
});
}
function delete_item(id) {
if (confirm('Are you sure delete this data?')) {
$.ajax({
url: "<?php echo site_url('ppmp/delete_item')?>/" + id,
type: "POST",
dataType: "JSON",
success: function(data) {
location.reload();
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error deleting data');
}
});
有问题的代码行是
$('#gcjmodal').on('click', edit_item('$item->id'));
将字符串传递给edit_item函数时,即“$item->id”
您需要用PHP标记解析它。因此,这条线应该是:
$('#gcjmodal').on('click', edit_item('<?php print $item->id ?>'));
什么是gcjmodal?调用上下文菜单时这是否可用,我这样问是因为在上下文菜单回调中,您正在将click事件处理程序附加到gcjmodal。@BhushanKawadkar,它是来自插件iziModal的模式。当我尝试编辑/删除时,这就是错误。邮递http://localhost/csc/ppmp/delete_item/$item-%3Eid 400错误请求,这是丢失的代码。你说得对,先生。感谢您在我尝试编辑时提供帮助。已提取最后一行数据。我想选择其他数据行。$item将始终引用数组中的最后一个元素。您需要动态地将每行的ID传递到edit_item函数中。您可以使用回调闭包中的item或ID吗?callback:functionitem,id{-所以您的代码应该是$'gcjmodal'。单击时,编辑_itemid;这是我的新代码行回调:functionitem,id{$'ppmptr'。单击时,编辑_itemid;返回true;}…我调用tableID,然后准确地调用tr。
$('#gcjmodal').on('click', edit_item('<?php print $item->id ?>'));