Bootstrap 3 Modal: how to check if modal is open or closed using jquery/javascript

后端 未结 5 1085
陌清茗
陌清茗 2021-02-01 21:15

Can anyone please tell me how to check bootstrap 3.0 modal status, is it open or closed using jQuery or javascript. I used following code but it works when you have opened a mod

相关标签:
5条回答
  • 2021-02-01 21:21

    try checking:

    if($("#addMemberModal").data('modal') && $("#addMemberModal").data('modal').isShown ) {
        console.log("Modal is open");
    }
    

    or

    if( $('#addMemberModal').hasClass('in') ) {
        console.log("Modal is open");
    }
    
    0 讨论(0)
  • 2021-02-01 21:21

    I used the following code for my AJAX Call. After a setTimeout i tried to close the model properly and used the is() method for doing so. Hope it helps somebody.

    complete: function(){
            setTimeout(function() {
                // model
                if($('#mdlNewOrder').is(':visible')) {
                    $('#mdlNewOrder').modal('toggle');
                }
                //buttons
                $('#btn_closeNewOrder').prop("disabled", false);
                $('#btn_NewOrder').prop("disabled", false);
                // table reload
                $('#table_patientlist').DataTable().ajax.reload(null, false);
            }, 2800);
        }
    
    0 讨论(0)
  • 2021-02-01 21:27

    You can also use straight jQuery like this:

    $('#myModal').is(':visible');
    
    0 讨论(0)
  • 2021-02-01 21:38

    if the modal is opened in a form loaded by ajax you can use

    $(document).on('hidden.bs.modal', '#photoModal', function () {
        cancel_camera();
        document.getElementById('PhotoPerson').innerHTML = '<img src="' +  photoPerson + '"/>';
    });*
    
    0 讨论(0)
  • 2021-02-01 21:39

    you can refer to their page http://getbootstrap.com/javascript/#modals

    $('#myModal').on('hidden.bs.modal', function (e) {
      // do something...
    })
    

    show.bs.modal
    This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.

    shown.bs.modal
    This event is fired when the modal has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the relatedTarget property of the event.

    hide.bs.modal
    This event is fired immediately when the hide instance method has been called.

    hidden.bs.modal This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete). loaded.bs.modal This event is fired when the modal has loaded content using the remote option.

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