获取JQuery UI弹出的实例

时间:2015-06-04 10:31:48

标签: jquery-ui dialog instance

我正在以这种方式创建一个JQuery UI弹出窗口:

function ShowJQueryStandardDialog(searchTarget, title, width, height, closeFunction)
{
    var $dialog = $('<div id="dialogDIV"><iframe id="dialogIFrame" frameborder="no" scrolling="auto" src="' + searchTarget + '" width="' + (width - 50) + 'px" height="' + (height - 50) + 'px"></iframe></div>');

    $dialog.dialog(
    {
        modal: true,
        title: title,
        show: 'slide',
        width: width,
        height: height,
        closeOnEscape: true,
        close: function (event, ui)
        {
            if (typeof closeFunction === 'function') closeFunction();
        }
    });
}

然后我想在代码中的其他位置获取弹出窗口的实例,以便我可以关闭它。我试过了:

var $dialog = $('#dialogDIV');
var $dialog = $('.ui-dialog');
var $dialog = $('.ui-dialog-content');

但它返回空对象。请注意,如果我将上面的代码放在对话框的close方法中,它可以正常工作。我可以得出结论,我无法从其初始代码外部访问弹出窗口吗?

1 个答案:

答案 0 :(得分:0)

找到方法:

function ShowJQueryStandardDialog(searchTarget, title, width, height, closeFunction)
{
    $dialog = $('<div id="dialogDIV"><iframe id="dialogIFrame" frameborder="no" scrolling="auto" src="' + searchTarget + '" width="' + (width - 50) + 'px" height="' + (height - 50) + 'px"></iframe></div>');

    $dialog.dialog(
    {
        modal: true,
        title: title,
        show: 'slide',
        width: width,
        height: height,
        closeOnEscape: true,
        close: function (event, ui)
        {
            if (typeof closeFunction === 'function') closeFunction();
        }
    });

    /* IMPORTANT BIT */
    $(document).bind('close-dialog', function ()
    {
        $dialog.dialog('close');
    });
}

如果要关闭对话框,请致电parent.$(parent.document).trigger('close-dialog');