Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 未在$.post方法中删除Datatables行_Jquery_Datatables - Fatal编程技术网

Jquery 未在$.post方法中删除Datatables行

Jquery 未在$.post方法中删除Datatables行,jquery,datatables,Jquery,Datatables,我有一段代码,当我单击所选行时,它可以正确地从表中删除一行 但是,当我将这一行嵌入到$.post.done中时,它根本不起作用: var table = $('#students-table').DataTable(); $('#students-table tbody').on('click', 'i.icon-delete', function () { var student_id = $(this).attr('student_id'); $.post("backend.

我有一段代码,当我单击所选行时,它可以正确地从表中删除一行

但是,当我将这一行嵌入到$.post.done中时,它根本不起作用:

var table = $('#students-table').DataTable();
$('#students-table tbody').on('click', 'i.icon-delete', function () {
    var student_id = $(this).attr('student_id');
    $.post("backend.php", {"action": "deleteStudent", "student_id": student_id}).done(function (response) {
        response = JSON.parse(response);
        if (response.deleted == "1") {
            console.log("A");
            table.row($(this).parents('tr')).remove().draw();
            console.log("B");
        }
    }); // post
});
但是,条件response.deleted==1的计算结果为True和console.logA;和console.logB;也可以正常工作。 那么,为什么行删除行在$.post的done函数中不起作用呢 请注意,问题不是关于删除过程。

在回调中,这指的是ajax调用的jqXHR对象,而不是事件处理程序绑定到的元素。因此,table.row$this.parents'tr'.remove.draw; 不起作用。
更多信息

根据预览回答:此内部“.done”指的是jqXHR,而不是选择器i.icon-delete的选定元素。 解决方案是将$this选择的选定元素i.icon-delete分配给$.post外部的变量,然后在$.post内部再次使用它。这样使用$This将引用i.icon-delete而不是jqXHR


开发人员控制台中出现任何错误?@esqew否没有错误,并且该表也是正确构造的@U25LYWT5IEJHC3RHCMG否的可能副本,而不是副本。我的问题都是关于$this的,不是关于删除过程,它本身可能是正确的。你能发布一个解决方案来完成你的回答吗?
var table = $('#students-table').DataTable();
$('#students-table tbody').on('click', 'i.icon-delete', function () {
    var student_id = $(this).attr('student_id');
    $.post("backend.php", {"action": "deleteStudent", "student_id": student_id}).done(function (response) {
        response = JSON.parse(response);
        if (response.deleted == "1") {
            console.log("A");
            table.row($(this).parents('tr')).remove().draw();
            console.log("B");
        }
    }); // post
});
$('#students-table tbody').on('click', 'i.icon-delete', function () {
    var rowToRemove = $(this).parents('tr');
    var student_id = $(this).attr('student_id');
    $.post("backend.php", {"action": "deleteStudent", "student_id": student_id}).done(function (response) {
        response = JSON.parse(response);
        if (response.deleted == "1") {
            table.row(rowToRemove).remove().draw();
        }
    }); // post
});