jquery ui:无法在初始化之前调用对话框上的方法;试图调用方法“关闭”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15501932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery ui: cannot call methods on dialog prior to initialization; attempted to call method 'close'
提问by hiway
I am using jquery ui dialog, I download it from jquery ui website, version is jquery-ui-1.10.2.custom.min.js, and jquery is jquery-1.9.1.js which is bundled with jquery ui js, but now I am encountering a question: when dialog is opened and click save button, I want the dialog to be closed, here is my code:
我正在使用 jquery ui 对话框,我从 jquery ui 网站下载它,版本是 jquery-ui-1.10.2.custom.min.js,jquery 是 jquery-1.9.1.js,它与 jquery ui js 捆绑在一起,但是现在我遇到一个问题:打开对话框并单击保存按钮时,我希望关闭对话框,这是我的代码:
$(function(){
$("#dialog-form").dialog({
autoOpen: false,
height: 350,
width: 450,
modal: true,
buttons: {
"save": function() {
if(!checkDept()){
return ;
}
$.post('dept_save.do',
{'dept.deptId':$("#dialog_dept_deptId").val(),
'dept.deptName':$("#dialog_dept_deptName").val(),
'dept.manager':$("#dialog_dept_manager").val(),
},function(data, status, xhr){
if(status == 'success'){
alert('save success');
$(this).dialog("close");
}else{
alert('error:'+data);
}
}
,"json");
}
},
close: function() {
$(this).dialog("close");
}
});
/* to open dialog*/
$("#add").click(function(){
$("#dialog-form").dialog("open");
});
now when I close the 'save success' popuped dialog, dialog-form
dialog was not closed, and an error occurs:
现在,当我关闭“保存成功”弹出对话框时,dialog-form
对话框没有关闭,并发生错误:
Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'close' jquery-1.9.1.js:507.
未捕获的错误:无法在初始化之前调用对话框上的方法;试图调用方法“关闭”jquery-1.9.1.js:507。
and there is another error:
还有另一个错误:
Uncaught SyntaxError: Unexpected token o jquery-1.9.1.js:541
Uncaught SyntaxError: Unexpected token o jquery-1.9.1.js:541
thanks.
谢谢。
回答by Mark Pieszak - Trilon.io
You are losing the context of this
once you are inside of $.post()
.
Before your $.post, save the context this
in a variable inside of that savebutton function.
您正在失去的情况下this
,一旦你的里面$.post()
。在 $.post 之前,将上下文保存在this
该保存按钮函数内的变量中。
$('#dialog-form').dialog({
// .....
buttons: {
'save': function() {
var $this = $(this);
// -this- is still the original context
// of $("#dialog-form")
$.post({
/// ...
$this.dialog('close'); // <-- used here
});
}
}
});
回答by JotaBe
When you initialize a dialog by calling the .dialog(options)
method, a new dialog is created, but it's not associated to the original div that you want to convert into dialog (the '#dialog-form' in your sample code). The dialog(options)
function returns the element containing the dialog data. So, if you do it this way:
当您通过调用该.dialog(options)
方法初始化对话框时,会创建一个新对话框,但它与您要转换为对话框的原始 div(示例代码中的“#dialog-form”)无关。该dialog(options)
函数返回包含对话框数据的元素。所以,如果你这样做:
var myDialog = $("#dialog-form").dialog(options);
You can then call the open method like this:
然后,您可以像这样调用 open 方法:
myDialog.dialog('open');
If you want to find the element contianing the dialog, it's the closest div with "ui-dialog-content" class:
如果你想找到包含对话框的元素,它是最接近“ui-dialog-content”类的 div:
var myDialog = $('#dialog-form').closest('div.ui-dialog-content');
The dialog data is the uiDialog
data which you'll see if you look at this element data:
对话框数据是uiDialog
您在查看此元素数据时将看到的数据:
myDialog.data();
回答by Kevin B
$(this)
isn't targeting the dialog inside of $.post
, you'll need to store a reference to it.
$(this)
不是针对 内部的对话框$.post
,您需要存储对它的引用。
var self = this; // add this
$.post(..., function(){
$(self).dialog("close"); // modify this to use self
回答by Amit
I had to do the following to get my dialog closed.
我必须执行以下操作才能关闭我的对话框。
$("#Note").dialog({
autoOpen: true,
modal: true,
create: function (e, ui) {
//
},
open: function () {
//
},
buttons: {
//
$(".ui-dialog-titlebar-close").trigger('click');
}
}
});
回答by dallin
Another thing that can cause this error is if for any reason you call removeData() on the dialog box. Apparently the jquery data() function is used to initialize the dialog, so if for any reason that data gets removed, the dialog will not think it has been initialized.
可能导致此错误的另一件事是,如果出于任何原因在对话框中调用 removeData()。显然 jquery data() 函数用于初始化对话框,因此如果由于任何原因数据被删除,对话框将不会认为它已被初始化。
I found this out when I was storing form data in the dialog object with the data() function and then called removeData() on the dialog right before closing it.
当我使用 data() 函数将表单数据存储在对话框对象中,然后在关闭对话框之前在对话框上调用 removeData() 时,我发现了这一点。
回答by user3768400
you might have used a popup instead of a dialog. I think the methods for closing the two are different. try use $("#popid").popup('close');
您可能使用了弹出窗口而不是对话框。我认为关闭两者的方法是不同的。尝试使用$("#popid").popup('close');