Detect if a jQuery UI dialog box is open

前端 未结 5 1791
离开以前
离开以前 2020-12-24 11:10

I am using a jQuery UI dialog. If it is open, I want to do one thing. If it is closed, I want to do another.

My question is, how do I detect if a jQuery UI dialog b

相关标签:
5条回答
  • 2020-12-24 11:42

    Nick Craver's comment is the simplest to avoid the error that occurs if the dialog has not yet been defined:

    if ($('#elem').is(':visible')) { 
      // do something
    }
    

    You should set visibility in your CSS first though, using simply:

    #elem { display: none; }
    
    0 讨论(0)
  • 2020-12-24 11:57

    If you want to check if the dialog's open on a particular element you can do this:

    if ($('#elem').closest('.ui-dialog').is(':visible')) { 
      // do something
    }
    

    Or if you just want to check if the element itself is visible you can do:

    if ($('#elem').is(':visible')) { 
      // do something
    }
    

    Or...

    if ($('#elem:visible').length) { 
      // do something
    }
    
    0 讨论(0)
  • 2020-12-24 12:02

    jQuery dialog has an isOpen property that can be used to check if a jQuery dialog is open or not.

    You can see example at this link: http://www.codegateway.com/2012/02/detect-if-jquery-dialog-box-is-open.html

    0 讨论(0)
  • 2020-12-24 12:06

    Actually, you have to explicitly compare it to true. If the dialog doesn't exist yet, it will not return false (as you would expect), it will return a DOM object.

    if ($('#mydialog').dialog('isOpen') === true) {
        // true
    } else {
        // false
    }
    
    0 讨论(0)
  • 2020-12-24 12:09

    If you read the docs.

    $('#mydialog').dialog('isOpen')
    

    This method returns a Boolean (true or false), not a jQuery object.

    0 讨论(0)
提交回复
热议问题