Twitter Bootstrap Modal - IsShown

2019-01-24 01:11发布

问题:

Hello Fellow SO users,

Got this problem where i auto populate a modal box.

Sometimes it already has content, so i tried doing a hide/show on each request. But the show is fired before the hide function is done, so it breaks the script.

I can't do a bind to "hidden", because if it's the first time - it won't fire the hidden function from bootstrap.

Using modal('true') i can see the object has a isShown element, but does anyone know how i can access it?

The console.log shows this:

$backdrop
    [div.modal-backdrop]

$element
    [div#modal-from-dom.modal]

isShown
    true

settings
    Object { backdrop="static", keyboard=true, show=false}

hide
    function()

show
    function()

toggle
    function()

__proto__
    Object { toggle=function(), show=function(), hide=function()}

回答1:

Answer for Twitter Bootstrap 3:

$("element").data()['bs.modal'].isShown

or

$("element").data('bs.modal').isShown


回答2:

The answer is:

$("element").data('modal').isShown


回答3:

On bootstrap 3.0.x

  $('#modal_Id').data().modal.isShown

or

  $('#modal_Id').data('modal').isShown

modal_id is the id of your modal



回答4:

If you'd like a Bootstrap version 2 and 3 solution and would rather not hit the data (since it looks like the name already changed once)...

$(element).hasClass('in') (would be "faded in" or "visible"; a plus that it returns a boolean)

or

"false" === $(element).attr('aria-hidden') (so that's aria-hidden or visible as well. "true" for hidden in this case.)

See source from bootstrap 3.3.1 here:

this.backdrop(function () {
...
  that.$element
    .addClass('in')
    .attr('aria-hidden', false)
...

Again, that code is from 3.3.1. Can confirm this also works in 2.1.0. Duck sniffing [sic] is probably better in this case.



回答5:

Answer for Bootstrap 4:

$("element").data('bs.modal')._isShown

As a function:

function isModalShown(modal) {
    var modalIsShown = (modal.data('bs.modal') || {})._isShown;
    return !(typeof modalIsShown === 'undefined' || !modalIsShown);
}