How to cancel jQuery UI dialog open?

安稳与你 提交于 2019-12-07 11:36:41

问题


I have the following:

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

How I should make it work?

Unfortunately there is no 'beforeopen' event to be hooked.


回答1:


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

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.

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.




回答2:


Look into autoOpen (set it to false) to keep it hidden until you confirm someCondition. Then call .dialog("open")




回答3:


The open event in dialog is like 'beforeopen'.



来源:https://stackoverflow.com/questions/3135721/how-to-cancel-jquery-ui-dialog-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!