简体   繁体   English

jQuery UI:模型对话框“关闭”

[英]jQuery UI: model dialog 'close'

I have 2 Razor views where one is loading the other in a jQuery UI dialog. 我有2个Razor视图,其中一个在jQuery UI对话框中加载另一个。 In the view that get loaded in the dialog; 在对话框中加载的视图中; I am opening another jQuery UI dialog to display a message. 我正在打开另一个jQuery UI对话框以显示一条消息。

The objective is to close both the dialogs when message dialog Cancel button is clicked. 目的是在单击消息对话框的“ Cancel按钮时关闭两个对话框。

Razor code is as follows: 剃刀代码如下:

Main View 主视图

<button id="openModel" onclick="openModel()">

<div id="mainDialog" />

<script type="text/javascript">
    function openModel() {
        $('#mainDialog').dialog({
            open: function () {
                // loading "the secondary view in the model dialog"
                // url: controller-action url to load the second view
                $(this).load('url'); 
            }
        });
    }
</script>

Dialog View 对话视窗

<button id="messageOpener">Verify</button>

<div id="messageDialog" />

<script type="text/javascript">

    $(document).ready(function () {

        $("#messageOpener").click(function () {
            $("#messageDialog").dialog("open");
                return false;
        });

        $("#messageDialog").dialog({
            buttons: {
                Retry: function () {
                    $(this).dialog("close");
                },
                Cancel: function () {
                    // **** ?? must close both dialogs. ****
                }                
            },
            autoOpen: false,
        });
    });

</script>

I have tried following approaches to close the dialogs: 我尝试了以下方法来关闭对话框:

Approach 01: 方法01:

$(".ui-dialog-content").dialog("close");

Approach 02: 方法02:

$(this).dialog("close");
$("#mainDialog").dialog("close");

Approach 03: 方法03:

$(".ui-dialog:visible").find(".dialog").dialog("close");

But all above does not close the dialogs as expected instead produces the following JS error: 但以上所有内容均未按预期关闭对话框,而是产生以下JS错误:

Uncaught Error: cannot call methods on dialog prior to initialization; 未捕获错误:无法在初始化之前在对话框上调用方法; attempted to call method 'close' 试图调用方法“关闭”
v.extend.error v.extend.error
(anonymous function) (匿名功能)
v.extend.each v.extend.each
v.fn.v.each vn.v.each
e.fn.(anonymous function) e.fn.(匿名函数)
$.dialog.buttons.Cancel $ .dialog.buttons.Cancel
r.click 点击
v.event.dispatch v.event.dispatch
o.handle.u o.handle.u

Re-writing the code in the following way solve the problem.. 通过以下方式重新编写代码即可解决问题。

1. Main View 1.主视图

<button id="openModel" onclick="openModel()">

<div id="mainDialog" />

<script type="text/javascript">

    var $modelDialog = $('#mainDialog').dialog({
        autoOpen: false,
        open: function () {
            // loading "the secondary view in the model dialog"
            // url: controller-action url to load the second view
            $(this).load('url'); 
        }
    });

    function openModel() {
        $modelDialog.dialog('open');    
    }

    function closeModel() {
        $modelDialog.dialog('close');    
    }

</script>

2. Dialog View 2.对话框视图

<button id="messageOpener">Verify</button>

<div id="messageDialog" />

<script type="text/javascript">

    var $messageDialog;

    $(document).ready(function () {

        $("#messageOpener").click(function () {
            $messageDialog.dialog("open");
                return false;
        });

        $messageDialog = $("#messageDialog").dialog({
            buttons: {
                Retry: function () {
                    $messageDialog.dialog("close");
                },
                Cancel: function () {
                    // [!!]
                    $messageDialog.dialog("destroy");
                    closeModel();
                }                
            },
            autoOpen: false,
        });
    });

</script>

Approach 2 looks fine , but you get that error cause the mainDialog modal is not opened when you try to close the message dialog. 方法2看起来不错,但是您遇到该错误,原因是当您尝试关闭message对话框时, mainDialog模态未打开。 Also, function openModel is not getting called anywhere. 同样,函数openModel不会在任何地方被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM