how to destroy bootstrap modal window completely?

后端 未结 24 734
旧时难觅i
旧时难觅i 2020-11-29 18:51

I\'ve made use of modal window for a wizard implementation which has around 4,5 steps. I need to destroy it completely after the last step(

相关标签:
24条回答
  • 2020-11-29 19:18

    For 3.x version

    $( '.modal' ).modal( 'hide' ).data( 'bs.modal', null );
    

    For 2.x version (risky; read comments below) When you create bootstrap modal three elements on your page being changed. So if you want to completely rollback all changes, you have to do it manually for each of it.

    $( '.modal' ).remove();
    $( '.modal-backdrop' ).remove();
    $( 'body' ).removeClass( "modal-open" );
    
    0 讨论(0)
  • 2020-11-29 19:19

    NOTE: This solution works only for Bootstrap before version 3. For a Bootstrap 3 answer, refer to this one by user2612497.

    What you want to do is:

    $('#modalElement').on('hidden', function(){
        $(this).data('modal', null);
    });
    

    that will cause the modal to initialize itself every time it is shown. So if you are using remote content to load into the div or whatever, it will re-do it everytime it is opened. You are merely destroying the modal instance after each time it is hidden.

    Or whenever you want to trigger the destroying of the element (in case it is not actually every time you hide it) you just have to call the middle line:

    $('#modalElement').data('modal', null);
    

    Twitter bootstrap looks for its instance to be located in the data attribute, if an instance exists it just toggles it, if an instance doesn't exist it will create a new one.

    Hope that helps.

    0 讨论(0)
  • 2020-11-29 19:19

    Also works on bootstrap 4.x

    $('#modal_ID').modal( 'hide' ).data( 'bs.modal', null );
    
    0 讨论(0)
  • 2020-11-29 19:20

    if is bootstrap 3 you can use:

    $("#mimodal").on('hidden.bs.modal', function () {
        $(this).data('bs.modal', null);
    });
    
    0 讨论(0)
  • 2020-11-29 19:20

    This worked for me.

    $('.modal-backdrop').removeClass('in');
    $('#myDiv').removeClass('in');
    

    The dialog and backdrop went away, but they came back the next time I clicked the button.

    0 讨论(0)
  • 2020-11-29 19:20

    With ui-router this may be an option for you. It reloads the controller on close so reinitializes the modal contents before it fires next time.

    $("#myModalId").on('hidden.bs.modal', function () {
      $state.reload();  //resets the modal
    });
    
    0 讨论(0)
提交回复
热议问题