我有下面的代码,它删除数据表的当前行。这段代码位于js文件中,并且在表所在的html文件中引用了js文件。
$(document).on('mousedown', '#orders', "td .fa.fa-minus-square", function () {
//get the description from the column whose class="description"
var description = $(this).closest('tr').find('.description').text();
//get the reference
var removeItemEl = $(this);
bootbox.confirm("Are you sure you want to delete this line item?", function (result) {
if (result) {
$.ajax({
url: "/api/mycontroller?description=" + description,
method: "DELETE",
success: function () {
table.row(removeItemEl.parents("tr")).remove().draw();
}
});
}
});
});
这里的问题是 $(this) 引用没有引用任何东西。如果我将此代码移回 html 文件,那么它就可以正常工作。如何访问该按钮所在的行?
最佳答案
将所有地方替换为 $(event.target),同时在第一行添加事件作为函数参数。
...function (event) ...
....
var description = $(event.target)
if ($(event.target) != //--insert desired element here
//cancel event handling if source is not desired element
return;
//source is desired element
....
关于javascript - 从另一个js文件获取数据表行的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52382655/