且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jquery ui对话框只打开一次

更新时间:2023-11-18 14:14:19

Scott Gonzalez(jQuery UI Team)谈到了很多人在开始使用jQuery UI时遇到这个问题的原因博文: http:// blog.nemikor.com/2009/04/08/basic-usage-of-the-jquery-ui-dialog/

Scott Gonzalez (of the jQuery UI Team) talks about the reason alot of people have this problem when getting started with jQuery UI in a recent blog post: http://blog.nemikor.com/2009/04/08/basic-usage-of-the-jquery-ui-dialog/

摘录:


用户经常遇到
对话框的问题是他们尝试
每次实例化一个新的对话框
用户执行某些操作
(通常单击链接或
按钮)。这是一个可以理解的
错误,因为乍看之下
似乎在
元素上调用.dialog()是导致对话框
打开。实际上发生的是
,一个新的对话实例正在创建
,然后该实例是

实例化之后立即打开。
对话框打开的原因是对话框有
一个autoOpen选项,默认为
true。因此,当用户在一个元素上调用.dialog()
两次时,第二次调用
被忽略,因为对话框的
已经在
元素上实例化。

The problem that users often encounter with dialogs is that they try to instantiate a new dialog every time the user performs some action (generally clicking a link or a button). This is an understandable mistake because at first glance it seems like calling .dialog() on an element is what causes the dialog to open. In reality what is happening is that a new dialog instance is being created and then that instance is being opened immediately after instantiation. The reason that the dialog opens is because dialogs have an autoOpen option, which defaults to true. So when a user calls .dialog() on an element twice, the second call is ignored because the dialog has already been instantiated on that element.

解决方案:

这个问题的简单解决方案是
实例化对话框
autoOpen set为false,然后在事件处理程序中调用
.dialog('open')。

The simple solution to this problem is to instantiate the dialog with autoOpen set to false and then call .dialog('open') in the event handler.



$(document).ready(function() {
    var $dialog = $('<div></div>')
        .html('This dialog will show every time!')
        .dialog({
            autoOpen: false,
            title: 'Basic Dialog'
        });

    $('#opener').click(function() {
        $dialog.dialog('open');
    });
});