【问题标题】:jQuery UI Dialog - Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'jQuery UI 对话框 - 未捕获的错误:无法在初始化之前调用对话框上的方法;试图调用方法“关闭”
【发布时间】:2014-04-20 18:57:56
【问题描述】:

我有这段代码:

function onRegisterPostSucces(data){
    $("#registerForm").dialog("close");
    $("#registerDialog").text(data);
    $("#registerDialog").dialog( "open" );

我想做的是从对话框表单发送注册数据,成功后我想关闭注册对话框并打开新对话框,其中包含有关创建新帐户的信息。 但是根据 dialog("close") 我收到了这个错误:

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

我尝试了以下解决方案:

$("#registerForm").dialog().dialog("close");

$("#registerForm").hide

但两者都没有关闭对话框 - 只隐藏对话框(例如输入),对话框仍然打开。我做错了什么?提前谢谢你。

更新初始化:

$("#registerDialog").dialog({autoOpen: false});
 $("#registerBar").dialog({autoOpen: false,
            modal: true,
            width: 400, height: 600, 
            buttons: {"Zamknij": function(){
                    $(this).dialog("close");
            }}});
(...)

在#registerBar 我有#registerForm:

$("#registerForm").submit(sendRegisterFormData);
function sendRegisterFormData(e){
    var contextPath='<%=request.getContextPath()%>';
    $.post(contextPath+"/login/AddUser", $("#registerForm").serialize(),
        onRegisterPostSucces);
    e.preventDefault();
}

而 onRegisterPostSuccess 我想关闭对话框。触发 POST 的按钮不是“对话框拥有”按钮。

【问题讨论】:

    标签: javascript jquery jquery-ui dialog jquery-ui-dialog


    【解决方案1】:

    我没有看到你在哪里实例化了你的对话框,就像这样(例如):

    $("#registerForm").dialog("close");

    function onRegisterPostSucces(data){
        $("#registerForm").hide();
        $("#registerBar").dialog('close');
        $("#registerDialog").text(data);
        $("#registerDialog").dialog( "open" );
    

    //这只是一个例子,你可能不需要这个。

     $("#registerForm").dialog({
         autoOpen: false,
         height: 300,
         width: 300,
         modal: true,
         // etc.. other things
         close: function() {
             $(this).dialog("close");
         }
       });
    

    我的猜测是你还没有实例化它或者'this'引用不正确?对不起,如果我偏离基础,因为我没有关注您的代码的全部范围。

    【讨论】:

    • 我更新了我的帖子。 “保存按钮”不是对话框的一部分,它是表单提交按钮。那是因为之前不是对话。您是否建议我将其设为“对话框拥有”按钮?我不确定,但我认为这可能会导致一些问题,因为那样它就不会是表单提交按钮。
    • 好的,但我很困惑的是:$("#registerForm").dialog("close");您正在尝试使用“this”作为“registerForm”的对话框插件。如果您只想在 onRegisterPostSucces 中添加回调,我更新了我的答案。
    • 对我感到羞耻 :) 你是对的,我尝试在“...Bar”上打开对话框并在“...Form”上关闭它,这就是它不起作用的原因。如果我改变它,它工作得很好 - 谢谢你的帮助! :)
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2013-03-10
    • 2014-05-29
    • 2014-04-13
    • 2013-02-21
    相关资源
    最近更新 更多