how to destroy bootstrap modal window completely?

后端 未结 24 732
旧时难觅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 18:53

    You can completely destroy a modal without page reloading by this way.

    $("#modal").remove();
    $('.modal-backdrop').remove();
    

    but it will completely remove modal from your html page. After this modal hide show will not work.

    0 讨论(0)
  • 2020-11-29 18:57

    I have tried accepted answer but it isn't seems working on my end and I don't know how the accepted answer suppose to work.

    $('#modalId').on('hidden.bs.modal', function () {
      $(this).remove();
    })
    

    This is working perfectly fine on my side. BS Version > 3

    0 讨论(0)
  • 2020-11-29 18:57

    The power of jQuery. $(selector).modal('hide').destroy(); will first remove sinds you might have the sliding affect and then removes the element completely, however if you want the user to be able to open the modal again after you finished the steps. you might just wanna update only the settings you wanna have reseted so for reseting all the inputs in your modal this would look like the following:

    $(selector).find('input, textarea').each(function(){
       $(this).val('');
    });
    
    0 讨论(0)
  • 2020-11-29 18:58

    This completely removes the modal from the DOM , is working for the "appended" modals as well .

    #pickoptionmodal is the id of my modal window.

    $(document).on('hidden.bs.modal','#pickoptionmodal',function(e){
    
    e.preventDefault();
    
    $("#pickoptionmodal").remove();
    
    });
    
    0 讨论(0)
  • 2020-11-29 18:59

    I have to destroy the modal right after it is closed through a button click, and so I came up with the following.

    $("#closeModal").click(function() {
        $("#modal").modal('hide').on('hidden.bs.modal', function () {
            $("#modal").remove();
        });
    });
    

    Note that this works with Bootstrap 3.

    0 讨论(0)
  • 2020-11-29 18:59

    only this worked for me

    $('body').on('hidden.bs.modal', '.modal', function() {
        $('selector').val('');
    });
    

    It is safe forcing selectors to make them blank since bootstrap and jquery version may be the reason of this problem

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