【问题标题】:How to load a Dialog JQuery UI twice when this dialog load a page dynamically?当此对话框动态加载页面时,如何加载对话框 JQuery UI 两次?
【发布时间】:2017-04-11 23:56:50
【问题描述】:

我有这个代码:

 $(function(){

          $('#dialog').dialog({
              autoOpen: false,
              closeOnEscape: false,
              modal: true,
              width: 450,
              buttons: {
                  "Enviar": function() {
                      $('#new_message').submit();
                  }
              }
          });

          $('#dialog_link').click(function(){
              $('#dialog').load('/messages/new');
              $('#dialog').dialog("open");
          });

      });

当我第一次点击时,对话框打开,但是,第二次没有打开,我知道问题是使用 .load 方法时,当我评论这段代码时,对话框当我点击链接时总是打开。

如何解决?

ps:我正在使用 Rails 3。

【问题讨论】:

    标签: jquery ruby-on-rails-3 jquery-ui-dialog


    【解决方案1】:

    由于 jQuery 对话框的非常奇怪的行为,以下是我发现处理这种情况的最佳方法:

    我这样构建我的 HTML:

    <div id="myDialog">
    </div>
    
    ... dynamically loaded HTML comes in a <div class="clearOnClose"></div> container
    

    然后是我的javascript:

    var myDialog = $('#myDialog');
    
    ...
    
    if (myDialog.is('.ui-dialog-content')) {
        myDialog.dialog('open');
    } else {
        myDialog.dialog({ modal: true, position: [200, 100], close: clearOnClose, etc. other options... });
    }
    
    ...
    
    // need to call on dialogs to make sure they don't interfere with each other
    var clearOnClose = function (event, ui) {
        jQuery(this).find('div.clearOnClose').html('');
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多