我正在编写对 python 后端的 ajax 调用,它为模式对话框生成 HTML,然后弹出所述对话框。但是,最近我在这里遇到了问题。我的代码(到目前为止)对于 JS 前端如下。
var deletegroup = function(group){
$.ajax({
url: "ajax.py/get_orphaned_networks",
data: {group: group},
success: function(result){
if (result == 0){
alert("Group does not contain networks, no networks will be deleted.");
} else {
$("#dgf_modal").html(result);
$("#dgf_modal").css('visibility', 'visible');
$("#dgf_modal").dialog({
modal: true,
closeOnEscape: false,
resizable: false,
draggable: false,
title: "Deleting Group",
width: 500,
height: 500,
buttons: {
"Yes" : function(){
alert("Well, okay then.");
$("#dgf_modal").dialog("close");
},
"Cancel" : function(){
$("#dgf_modal").dialog("close");
}
},
close: function(){
$("#dgf_modal").html("");
$("#dgf_modal").css('visibility', 'hidden');
$(this).dialog("destroy");
}
});
$("#dgf_modal").dialog("open");
}
},
error: function(){
setlongmessage("An error has occurred. Contact your system administrator.");
$("#dgf_status").html("<img src='images/error.png' alt='An error occured.'/>");
setTimeout(function(){$("#dgf_status").html("");}, 3000);
}
});
}
我在我的应用程序的另一部分中有类似的代码,它工作得很好。但是,对于这个,我收到以下错误:
Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'
throw new Error( msg );
jquery.js (line 477)
编辑:如果我在 div 上调用 dialog("open") 或 dialog("close"),我会收到相同的错误消息。相同的堆栈跟踪,唯一改变的错误是它试图调用方法“关闭”而不是“打开”。
我使用的是最新版本的 jQueryUI 和 jQuery,因为我认为更新它们或许可以解决这个问题。没有骰子。也许我在这个函数的某个地方搞砸了,但我确实知道 ajax 调用返回了它应该返回的正确 HTML。
检查其他所有内容 - 我确实有 ID 为 dgf_modal 的 div,并且工作正常。不过我现在不太确定是什么问题。
最佳答案
没有理由调用open,使用autoOpen创建对话框时的选项。
关于javascript - jQueryUI 无法打开模态对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13278693/