How can I tell if a jquery ui dialog query has been initialized?

ⅰ亾dé卋堺 提交于 2020-01-02 02:51:09

问题


I have the following code to detect if a jquery ui dialog is open:

if ($("#dialog-myDialog").dialog("isOpen")) {
      return;
}

which works fine but I found a situation where this code get called prior to the dialog being initialized in the first place and this if statement seems to just blow up in this case.

What is the best way to check if a jquery ui dialog has been initialized so I can properly handle this situation.


回答1:


Test whether the element has the ui-dialog-content class:

if ($("#dialog-myDialog").hasClass("ui-dialog-content") &&
    $("#dialog-myDialog").dialog("isOpen")) {
    return;
}



回答2:


If you use a solution which relies on the presence of a css class added by a component that is out of your control, then you run the risk of this not working if a new version of the component changes the way it manages classes.

A more reliable solution would be to add your own existence indicator at dialog initialization:

$("#popup").attr("_dialogInitialized", "yes").dialog( { ... } )

Then check for your indicator when you need to:

if ($("#popup[_dialogInitialized]").length == 1) {
    // dialog has been previously initialized
} else {
    // dialog has been not yet been initialized
}



回答3:


Add a class when initilizing:

$("selector").addClass("initialized").dialog( { ... } );

Then check for the class when needed:

if ($("selector").hasClass("initialized")) { ... }


来源:https://stackoverflow.com/questions/29528706/how-can-i-tell-if-a-jquery-ui-dialog-query-has-been-initialized

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