如何从对话框代码内部关闭 jquery ui 对话框?

发布于 2024-09-25 17:26:46 字数 1542 浏览 5 评论 0原文

我在应用程序中使用多个 jquery ui 对话框来收集用户的输入并进行 CRUD 操作。当在页面中我需要一个对话框时,我执行以下步骤:

首先定义初始化对话框所需的最少标记和代码

<div id="dialogPanel" style="display:none" title=""></div>

,当 DOM 准备好时,

$("#dialogPanel").dialog({
    autoOpen: false, hide: 'slide', show: 'bounce', resizable: false, 
    position: 'center', stack: true, height: 'auto', width: 'auto', 
    modal: true, overlay: { opacity: 0.5, background: "white" }
});

然后在需要时使用 ajax 加载对话框中的内容调用,像这样

<button id="addCustomer">Add Customer</button>

和这样

$("#addCustomer").button().click(function(event) {
    event.preventDefault();
    loadDialog("/Customer/Create", "", "Add New Customer");
});

function loadDialog(action, id, title) {
    $("#dialogPanel").dialog("option", "title", title);
    if (id != "") 
        action = action + "/" + id;
    $.ajax({
        type: "get",
        dataType: "html",
        url: action,
        data: {},
        success: function(response) {
            $("#dialogPanel").html('').html(response).dialog('open');
        }
    });
}

ajax 调用返回一个强类型视图,它将显示在对话框内,甚至可以动态调整其大小。强类型视图有一些按钮,这些按钮依次执行其他 ajax 调用(例如验证并保留在数据库上的控制器操作)。这些调用通常返回将添加到对话框 DIV 中的 HTML 代码。

现在我的问题是:如何关闭对话框?我可以通过单击对话框右上角的“X”按钮或按 ESC 键来完成此操作,但我想通过单击强类型视图内的按钮来完成此操作。

使用此类代码

$("#dialogPanel").dialog('close');

但是,我不希望在本身未定义 #dialogPanel DIV 的强类型视图中

。什么可能是一个好的解决方案?

I use several jquery ui dialogs in my application to collect input from users and for CRUD operations. When in a page I need a dialog I do the following steps:

First I define the minimum markup and code necessary to initialize the dialog like this

<div id="dialogPanel" style="display:none" title=""></div>

and, when the DOM is ready,

$("#dialogPanel").dialog({
    autoOpen: false, hide: 'slide', show: 'bounce', resizable: false, 
    position: 'center', stack: true, height: 'auto', width: 'auto', 
    modal: true, overlay: { opacity: 0.5, background: "white" }
});

Then I load content in the dialog when I need it using ajax calls, like this

<button id="addCustomer">Add Customer</button>

and this

$("#addCustomer").button().click(function(event) {
    event.preventDefault();
    loadDialog("/Customer/Create", "", "Add New Customer");
});

function loadDialog(action, id, title) {
    $("#dialogPanel").dialog("option", "title", title);
    if (id != "") 
        action = action + "/" + id;
    $.ajax({
        type: "get",
        dataType: "html",
        url: action,
        data: {},
        success: function(response) {
            $("#dialogPanel").html('').html(response).dialog('open');
        }
    });
}

The ajax call returns a strong typed view that will show inside the dialog and even resize it dinamically. The strong typed view has some buttons that, in turns, does other ajax calls (for example to the controller action that validate and persist on the database). These calls usually returns back HTML code that will be added to the dialog DIV.

Now my question is: How do I close the dialog? I can do it by clicking the "X" button on top right corner of the dialog or by pressing the ESC key but I would like to do it as a consequence of the click to the button that is inside the strong typed view .

However I do not wish to use this kind of code

$("#dialogPanel").dialog('close');

inside a strong typed view that does not defines the #dialogPanel DIV in itself.

What could be a good solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2024-10-02 17:26:46

在主窗体中创建 closeDialog 函数,并从对话框中调用该函数。然后托管该对话框的任何其他页面也需要定义“closeDialog”函数。

另外,您可以传递一个指向 closeDialog 函数的委托来进一步分隔事物。

Create a closeDialog function in the main form, and call that function from within the dialog. Then any other page that hosts the dialog also needs to define a "closeDialog" function.

Also, you could pass a delegate pointing to the closeDialog function to separate things further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文