简体   繁体   English

jquery ui对话框只打开一次

[英]jquery ui dialog opens only once

I have a button that opens a dialog when clicked. 我有一个按钮,单击时会打开一个对话框。 The dialog displays a div that was hidden 该对话框显示隐藏的div

After I close the dialog by clicking the X icon, the dialog can't be opened again. 单击X图标关闭对话框后,无法再次打开对话框。

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/ Scott Gonzalez(jQuery UI团队)在最近的博客文章中讨论了很多人在开始使用jQuery UI时遇到此问题的原因: http//blog.nemikor.com/2009/04/08/basic-usage -of最jQuery的UI的对话框/

An excerpt: 摘录:

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. 这是一个可以理解的错误,因为乍一看似乎在元素上调用.dialog()是导致对话框打开的原因。 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. 对话框打开的原因是对话框具有autoOpen选项,默认为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. 因此,当用户对元素两次调用.dialog()时,将忽略第二个调用,因为该对话框已在该元素上实例化。

Solution: 解:

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. 解决此问题的简单方法是将autoOpen设置为false的对话框实例化,然后在事件处理程序中调用.dialog('open')。

$(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');
    });
});

Are you using the ui dialog? 你在使用ui对话框吗? You should post some code so we can see what is causing your problem. 您应该发布一些代码,以便我们可以看到导致问题的原因。 But here iss a guess (because I made this same mistake recently). 但这里有一个猜测(因为我最近犯了同样的错误)。 When using ui dialog you have to initialize the dialog only once. 使用ui对话框时,您只需初始化对话框一次。

 $(document).ready(function() {
   $('#dialog').dialog({
     autoOpen:false,
     modal:'true',
     width:450,
     height:550
  });
 $('#MyButton').click(openDialog);    

});

This code, initializes the dialog but does not show it. 此代码初始化对话框但不显示对话框。 The openDialog function will then open the dialog box when MyButton is clicked. 单击MyButton时,openDialog函数将打开对话框。

   var openDialog = function(){

       $('#dialog').load('loadurl.php');//load with AJAX

      //optionally change options each time it is clicked
       $('#dialog').dialog('option', 'buttons',{
          "Cancel":function(){
             $('#dialog').dialog('close');
          }
      });

     //actually open the dialog
     $('#dialog').dialog('open');

};

As an addition to the accepted answer i would like to add that you need to pay attention when using this in an asp.net updatepanel. 作为已接受答案的补充,我想补充一点,在asp.net更新面板中使用时需要注意。 If you click the button then a postback will happen, the popup will open but won't open a second time because of the postback that happened. 如果单击该按钮,则会发生回发,弹出窗口将打开但不会再次打开,因为发生了回发。 So you should make sure that the button you use to open the popup does not postback. 因此,您应该确保用于打开弹出窗口的按钮不会回发。 ie: 即:

<asp:LinkButton ID="btn1" runat="server" OnClientClick="return false;">Click me</asp:LinkButton>

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

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