删除 ajax 回调问题

Deletion on the ajax callback issue

本文关键字:问题 回调 ajax 删除      更新时间:2023-09-26

我的网站中有一个仪表板,其中包含表中的一些条目,表格中的每个条目都有一个delete按钮。当用户单击delete按钮时,会发生Ajax调用,我正在回调函数中删除该条目。我的代码 :

$(".del").live({
    click: function () {
        $.post("/Home/DeleteTemplate", {
            name: $(this).parent().siblings("td:first").children("a").html()
        }, function (data) {
            $(this).parents("tr").remove(); //Inside the callback
        });
    }
});

现在我的问题是,如果我删除回调函数中的行,该行不会立即从条目中删除。我必须关闭并再次打开仪表板才能看到结果。

但是,如果我删除回调函数之外的条目,则它同时删除:

$(".del").live({
    click: function () {
        $.post("/Home/DeleteTemplate", {
            name: $(this).parent().siblings("td:first").children("a").html()
        });
        $(this).parents("tr").remove(); //Outside the callback   
    }
});

这里有什么问题?

this 在回调中没有引用你的选择器,这样做:

$(".del").live({ click: function () { 
  var that = $(this); // added this
  $.post("/Home/DeleteTemplate", 
        { name: that.parent().siblings("td:first").children("a").html() }, 
        function (data) { 
        that.parents("tr").remove(); // used "that" here
        }); 
  } 
});