Bootstrap: Open Another Modal in Modal

后端 未结 21 847
难免孤独
难免孤独 2020-11-27 11:28

So, I\'m using this code to open another modal window in a current opened modal window:



        
相关标签:
21条回答
  • 2020-11-27 11:35

    Update for 2019 -- Using Bootstrap 4

    I found most of the answers seem to have a lot of unnecessary jQuery. To open a modal from another modal can be done simply by using the events that Bootstrap provides such as show.bs.modal. You may also want some CSS to handle the backdrop overlays. Here are the 3 open modal from another scenarios...

    Open modal from another modal (keep 1st modal open)

    <a data-toggle="modal" href="#myModal" class="btn btn-primary">Launch modal</a>
    
    <div class="modal" id="myModal">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title">Modal title</h4>    
                  <button type="button" class="close" data-dismiss="modal">×</button>
                </div><div class="container"></div>
                <div class="modal-body">
                  ...
                  <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Open modal2</a>
                </div>
                <div class="modal-footer">
                  <a href="#" data-dismiss="modal" class="btn">Close</a>
                </div>
              </div>
            </div>
    </div>
    <div class="modal" id="myModal2" data-backdrop="static">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title">2nd Modal title</h4>
                  <button type="button" class="close" data-dismiss="modal">×</button>
                </div><div class="container"></div>
                <div class="modal-body">
                  ..
                </div>
                <div class="modal-footer">
                  <a href="#" data-dismiss="modal" class="btn">Close</a>
                  <a href="#" class="btn btn-primary">Save changes</a>
                </div>
              </div>
            </div>
    </div>
    

    A potential issue in this case is that the backdrop from the 2nd modal hides the 1st modal. To prevent this, make the 2nd modal data-backdrop="static", and add some CSS to fix the z-indexes of the backdrops...

    /* modal backdrop fix */
    .modal:nth-of-type(even) {
        z-index: 1052 !important;
    }
    .modal-backdrop.show:nth-of-type(even) {
        z-index: 1051 !important;
    }
    

    https://www.codeply.com/go/NiFzSCukVl


    Open modal from another modal (close 1st modal)

    This is similar to the above scenario, but since we are closing the 1st modal when the 2nd is opened, there is no need for the backdrop CSS fix. A simple function that handles the 2nd modals show.bs.modal event closes the 1st modal.

    $("#myModal2").on('show.bs.modal', function (e) {
        $("#myModal1").modal("hide");
    });
    

    https://codeply.com/go/ejaUJ4YANz


    Open modal inside another modal

    The last multiple modals scenario is opening the 2nd modal inside the 1st modal. In this case the markup for the 2nd is placed inside the 1st. No extra CSS or jQuery is needed.

    <div class="modal" id="myModal1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Modal title</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                </div>
                <div class="container"></div>
                <div class="modal-body">
                    ...
                    <a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal 2</a>
                </div>
                <div class="modal-footer">
                    <a href="#" data-dismiss="modal" class="btn">Close</a>
                </div>
            </div>
        </div>
    
        <div class="modal" id="myModal2">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">2nd Modal title</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    </div>
                    <div class="container"></div>
                    <div class="modal-body">
                        ...
                    </div>
                    <div class="modal-footer">
                        <a href="#" data-dismiss="modal" class="btn">Close</a>
                        <a href="#" class="btn btn-primary">Save changes</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    https://codeply.com/go/iSbjqubiyn

    0 讨论(0)
  • 2020-11-27 11:37

    Twitter docs says custom code is required...

    This works with no extra JavaScript, though, custom CSS would be highly recommended...

    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <!-- Button trigger modal -->
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modalOneModal">
          Launch demo modal
        </button> 
                <!-- Modal -->
                <div class="modal fade bg-info" id="modalOneModal" tabindex="-1" role="dialog" aria-labelledby="modalOneLabel" aria-hidden="true">
        
                  <div class="modal-dialog">
              
                    <div class="modal-content  bg-info">
                      <div class="modal-header btn-info">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="modalOneLabel">modalOne</h4>
                      </div>
                      <div id="thismodalOne" class="modal-body bg-info">
                    
                    
                  <!-- Button trigger modal -->
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#twoModalsExample">
          Launch demo modal
        </button>
                 
                        <div class="modal fade bg-info" id="twoModalsExample" style="overflow:auto" tabindex="-1" role="dialog" aria-hidden="true">
                    <h3>EXAMPLE</h3>
                </div>
                      </div>
                      <div class="modal-footer btn-info" id="woModalFoot">
                        <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
                      </div>
                    </div>
                  </div>
                </div>
        <!-- End Form Modals -->

    0 讨论(0)
  • 2020-11-27 11:40

    I went kind of a different route all together, I decided to "De-Nest" them. Maybe someone will find this handy...

    var $m1 = $('#Modal1');
    var $innermodal = $m1.find(".modal");     //get reference to nested modal
    $m1.after($innermodal);                  // snatch it out of inner modal and put it after.
    
    0 讨论(0)
  • 2020-11-27 11:40

    $(document).on('hidden.bs.modal', function (event) {
      if ($('.modal:visible').length) {
        $('body').addClass('modal-open');
      }
    });

    0 讨论(0)
  • 2020-11-27 11:41

    My solution does not close the lower modal, but truly stacks on top of it. It preserves scrolling behavior correctly. Tested in Bootstrap 3. For modals to stack as expected, you need to have them ordered in your Html markup from lowest to highest.

    $(document).on('hidden.bs.modal', function (event) {
      if ($('.modal:visible').length) {
        $('body').addClass('modal-open');
      }
    });

    UPDATE: When you have stacked modals, all the backdrops appear below the lowermost modal. You can fix that by adding the following CSS:

    .modal-backdrop {
        visibility: hidden !important;
    }
    .modal.in {
        background-color: rgba(0,0,0,0.5);
    }
    

    This will give the appearance of a modal backdrop below the topmost visible modal. That way it grays out your lower modals underneath.

    0 讨论(0)
  • 2020-11-27 11:42

    I also had some trouble with my scrollable modals, so I did something like this:

      $('.modal').on('shown.bs.modal', function () {
        $('body').addClass('modal-open');
        // BS adds some padding-right to acomodate the scrollbar at right
        $('body').removeAttr('style');
      })
    
      $(".modal [data-toggle='modal']").click(function(){
        $(this).closest(".modal").modal('hide');
      });
    

    It will serve for any modal whithin a modal that comes to appear. Note that the first its closed so the second can appear. No changes in the Bootstrap structure.

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