【问题标题】:jquery dialog close not working "cannot call methods on dialog prior to initialization; attempted to call method"jquery对话框关闭不起作用“无法在初始化之前调用对话框上的方法;试图调用方法”
【发布时间】:2014-04-13 16:05:33
【问题描述】:

我的 jquery 对话框关闭时出现以下错误。 jQuery 1.9.1 jquery-ui-1.10.3

错误:无法在初始化之前调用对话框上的方法;试图调用方法'close'

// 检查给定的回调是否在列表中。

    var popupDialog = $("<div id='popupDialogBox'></div>");
function Popup(url, width, height) {
    if (width == undefined)
        width = 400;
    if (height == undefined)
        height = 300;
    //var buttonArray = {};
    //buttonArray["Close"] = function () {
    //    $(popupDialog).dialog("close");
    //};
    popupDialog.dialog({
        closeOnEscape: false,
        modal: true,
        autoOpen: false,
        resizable: false,
        buttons: [{
            text: "Cancel",
            click: function () {
                $(popupDialog).dialog("close");
            }
        }]
        //buttons: buttonArray,
        //buttons: {
        //    "OK": function () {
        //        //debugger;
        //        //alert('test');
        //        //$(popupDialog).parent().remove();
        //        $(this).dialog("close");
        //        return false;
        //    }
        //}
        //buttons: [{
        //    text: "close",
        //    "class": "button",
        //    close: function () {
        //        $(popupDialog).dialog('close');
        //        //alert('test');
        //    }
        //}]
    });
    popupDialog.load(url + '?popup=1');
    popupDialog.dialog("option", "width", width);
    popupDialog.dialog("option", "height", height);
    popupDialog.dialog('open');
    return false;
}

如果我在 $(popupDialog).dialog('close') 之前放置警报,则警报有效,但对话框仍然给出相同的错误

不确定我在这里做错了什么,但似乎没有任何效果。

有什么想法吗?

【问题讨论】:

    标签: javascript jquery jquery-ui-dialog


    【解决方案1】:

    您必须先将 div 添加到 DOM。

    改变 var popupDialog = $("<div id='popupDialogBox'></div>");" 变成这样: $("html").append("<div id='popupDialogBox' />"); var popupDialog = $("#popupDialogBox");

    然后继续你的代码。

    【讨论】:

    • 我只是注意到它没有正确执行 HTML。哈哈。我还在学习stackoverflow的用法。希望你明白我在说什么。
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 2013-03-08
    • 2013-03-10
    • 2013-02-21
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2013-07-27
    相关资源
    最近更新 更多