Bootstrap modal: close current, open new

前端 未结 27 1669
离开以前
离开以前 2020-11-29 00:23

I have looked for a while, but I can\'t find a solution for this. I want the following:

  • Open an URL inside a Bootstrap modal. I have this working off course. S
相关标签:
27条回答
  • 2020-11-29 01:00

    Simple and elegant solution for BootStrap 3.x. The same modal can be reused in this way.

    $("#myModal").modal("hide");
    $('#myModal').on('hidden.bs.modal', function (e) {
       $("#myModal").html(data);
       $("#myModal").modal();
       // do something more...
    }); 
    
    0 讨论(0)
  • 2020-11-29 01:03

    Just copy and paste this code and you can experiment with two modals. Second modal is open after closing the first one:

    <!-- Button to Open the Modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
        Open modal
    </button>
    
    <!-- The Modal -->
    <div class="modal" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">
    
                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">Modal Heading</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
    
                <!-- Modal body -->
                <div class="modal-body">
                    Modal body..
                </div>
    
                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
    
            </div>
        </div>
    </div>
    
    
    
    <!-- The Modal 2 -->
    <div class="modal" id="myModal2">
        <div class="modal-dialog">
            <div class="modal-content">
    
                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">Second modal</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
    
                <!-- Modal body -->
                <div class="modal-body">
                    Modal body..
                </div>
    
                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
    
            </div>
        </div>
    </div>
    
    
    
    
    <script>
        $('#myModal').on('hidden.bs.modal', function () {
            $('#myModal2').modal('show')
        })
    </script>
    

    Cheers!

    0 讨论(0)
  • 2020-11-29 01:04

    Hide modal dialog box.

    Method 1: using Bootstrap.

    $('.close').click(); 
    $("#MyModal .close").click();
    $('#myModalAlert').modal('hide');
    

    Method 2: using stopPropagation().

    $("a.close").on("click", function(e) {
      $("#modal").modal("hide");
      e.stopPropagation();
    });
    

    Method 3: after shown method invoke.

    $('#myModal').on('shown', function () {
      $('#myModal').modal('hide');
    })
    

    Method 4: Using CSS.

    this.display='block'; //set block CSS
    this.display='none'; //set none CSS after close dialog
    
    0 讨论(0)
  • 2020-11-29 01:05

    Without seeing specific code, it's difficult to give you a precise answer. However, from the Bootstrap docs, you can hide the modal like this:

    $('#myModal').modal('hide')
    

    Then, show another modal once it's hidden:

    $('#myModal').on('hidden.bs.modal', function () {
      // Load up a new modal...
      $('#myModalNew').modal('show')
    })
    

    You're going to have to find a way to push the URL to the new modal window, but I'd assume that'd be trivial. Without seeing the code it's hard to give an example of that.

    0 讨论(0)
  • 2020-11-29 01:05

    With BootStrap 3, you can try this:-

    var visible_modal = jQuery('.modal.in').attr('id'); // modalID or undefined
    if (visible_modal) { // modal is active
        jQuery('#' + visible_modal).modal('hide'); // close modal
    }
    

    Tested to work with: http://getbootstrap.com/javascript/#modals (click on "Launch Demo Modal" first).

    0 讨论(0)
  • 2020-11-29 01:07

    You need to add following attribute to the link or button where you want add this functionality:

     data-dismiss="modal" data-toggle="modal" id="forgotPassword" data-target="#ModalForgotPassword"
    

    A detaile blog: http://sforsuresh.in/bootstrap-modal-window-close-current-open-new-modal/

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