gpt4 book ai didi

javascript - 无法在ajax成功中关闭对话框

转载 作者:搜寻专家 更新时间:2023-11-01 04:52:51 25 4
gpt4 key购买 nike

我正在动态调用对话框(单击时),而不是先将其设置为 var。像这样:

$(".deleteSaved").click(function() {
save_id = $(this).attr('id');


div="<div>Are you sure you want to delete this?</div>";
$(div).dialog({
buttons: {
"Delete": function() {
$.ajax ({
url:"util.php",
data:"q=0&f=delete&save_id="+save_id,
success: function(result){
$(this).dialog("close"); //this line is not working
$("#toprow"+save_id).fadeOut();
$("#botrow"+save_id).fadeOut();
}
})
},
"Cancel": function() {

$(this).dialog("close");
}
},
modal: true,
title: 'Delete Saved Signal',
resizable: false
});
});

但是当我在 ajax success 函数中调用 $(this).dialog("close"); 时,出现以下错误:

Uncaught cannot call methods on dialog prior to initialization; attempted to call method 'close'

在“取消”按钮中 $(this).dialog("close"); 工作得很好。

如何在 ajax 成功调用中使用关闭函数?

最佳答案

在 success 函数中,'this' 没有指向对话框对象。您可能必须将对话框对象存储在另一个变量中,如下所示

"Delete": function() { 
var that = this;
$.ajax ({
url:"util.php",
data:"q=0&f=delete&save_id="+save_id,
success: function(result){
$(that).dialog("close"); //this line will work
$("#toprow"+save_id).fadeOut();
$("#botrow"+save_id).fadeOut();
}
})
},

关于javascript - 无法在ajax成功中关闭对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8237088/

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