【问题标题】:How to cancel jQuery UI dialog open?如何取消打开 jQuery UI 对话框?
【发布时间】:2010-06-28 20:01:27
【问题描述】:

我有以下几点:

container.dialog().bind('dialogopen', function(event, ui)
  {
     ...
     if (someCondition)
     {
         $(this).dialog('close'); // the dialog is not closed!
     }
  }

我应该如何让它工作?

很遗憾,没有要挂钩的 'beforeopen' 事件。

【问题讨论】:

    标签: jquery jquery-ui dialog show


    【解决方案1】:

    这里的问题是您需要在事件发生之前绑定事件。目前,您正在调用 .dialog() 以打开对话框(除非 autoOpen: false 是提供的选项)。这意味着在您的 .bind(....) 运行之前,该事件已经发生。解决方案是在事件发生之前绑定,如下所示:

    container.bind('dialogopen', function(event, ui) {
      if (someCondition) {
         $(this).dialog('close'); // the dialog is not closed!
      }
    }).dialog(); //.dialog() fires "dialogopen" as part of it's execution
    

    You can view a demonstration here,它会阻止对话框打开,或者说它确实打开了,但在 UI 线程更新之前立即关闭,所以对用户来说,它永远不会打开。

    这是有效的,因为the dialogopen event 是在您变成对话框的元素(而不是对话框容器)上触发的...稍后创建对话框本身并不重要,这只是一个侦听事件的 DOM 元素.

    【讨论】:

      【解决方案2】:

      查看autoOpen(将其设置为false)以使其隐藏,直到您确认某些条件。然后拨打.dialog("open")...

      【讨论】:

      • 是的。我想到了这一点,但由于它是一个插件,我无法确定 .dialog('open') 的调用者是谁。也许我应该重新考虑我的设计。
      【解决方案3】:

      对话框中的打开事件类似于'beforeopen'。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-10
        • 2012-12-04
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多