如何取消jQuery UI对话框打开? [英] How to cancel jQuery UI dialog open?
问题描述
我有以下内容:
container.dialog().bind('dialogopen', function(event, ui)
{
...
if (someCondition)
{
$(this).dialog('close'); // the dialog is not closed!
}
}
我应该如何运作?
不幸的是没有'beforeopen'事件挂钩。
Unfortunately there is no 'beforeopen' event to be hooked.
推荐答案
你需要在之前绑定之前的事件,目前你正在调用 .dialog()
打开对话框(除非 autoOpen:false
是提供的选项),这意味着在您的 .bind(....)
运行,事件已经发生了,解决方案是在之前绑定事件发生,如下所示:
The problem here is you need to bind the event before it happens. Currently you're calling .dialog()
which opens the dialog (unless autoOpen: false
is a provided option). This means before your .bind(....)
runs, the event already occured. The solution is to just bind before the event happens, like this:
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
你可以在这里查看一个演示,它会阻止对话框打开,或者它打开,但是在UI线程更新之前立即关闭,所以对用户来说,它永远不会打开。
You can view a demonstration here, it'll prevent the dialog from opening, or rather it does open, but instantly closes before the UI thread updates, so to the user, it never opened.
这是因为 dialogopen
事件被触发了您转换成对话框的元素(而不是对话框容器)...稍后再创建对话框本身并不重要只是一个监听事件的DOM元素。
This works because the dialogopen
event is fired on the element you turned into a dialog (not the dialog container)...it doesn't matter that the dialog itself is created later, this is just a DOM element listening to events.
这篇关于如何取消jQuery UI对话框打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!