如何取消打开jQuery UI对话框?

时间:2010-06-28 20:01:27

标签: jquery jquery-ui dialog show

我有以下内容:

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

我应该如何运作?

不幸的是,没有'beforeopen'事件被挂钩。

3 个答案:

答案 0 :(得分:4)

这里的问题是你需要绑定事件才能发生。目前您正在调用.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元素侦听事件。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

对话框中的打开事件就像'beforeopen'。