gpt4 book ai didi

javascript - 关于ajax回调问题的删除

转载 作者:行者123 更新时间:2023-11-29 18:28:05 26 4
gpt4 key购买 nike

我的网站上有一个仪表板,其中包含表格中的一些条目,表格中的每个条目都有一个 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
});
}
});

关于javascript - 关于ajax回调问题的删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11099912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com