Bootstrap: Open Another Modal in Modal

后端 未结 21 848
难免孤独
难免孤独 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:45

    Modals in Modal:

    $('.modal-child').on('show.bs.modal', function () {
        var modalParent = $(this).attr('data-modal-parent');
        $(modalParent).css('opacity', 0);
    });
     
    $('.modal-child').on('hidden.bs.modal', function () {
        var modalParent = $(this).attr('data-modal-parent');
        $(modalParent).css('opacity', 1);
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    <a href="#myModal" role="button" class="btn btn-primary" data-toggle="modal">Modals in Modal</a>
    
    
    <div id="myModal" class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                 
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Modal Header</h4>
                </div>
                <div class="modal-body">
                    <a href="#myModal1" role="button" class="btn btn-primary" data-toggle="modal">Launch other modal 1</a>
                    <a href="#myModal2" role="button" class="btn btn-primary" data-toggle="modal">Launch other modal 2</a>
                </div>
    
            </div>
        </div>
    </div>
    
    <div id="myModal1" class="modal modal-child" data-backdrop-limit="1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-modal-parent="#myModal">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Modal Header 1</h4>
                </div>
                <div class="modal-body">
                    <p>Two modal body…1</p>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal" data-dismiss="modal" aria-hidden="true">Cancel</button>
                </div>
    
            </div>
        </div>
    </div>
    
    <div id="myModal2" class="modal modal-child" data-backdrop-limit="1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-modal-parent="#myModal">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Modal Header 2</h4>
                </div>
                <div class="modal-body">
                    <p>Modal body…2</p>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal" data-dismiss="modal" aria-hidden="true">Cancel</button>
                </div>
    
            </div>
        </div>
    </div>

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

    Why not just change the content of the modal body?

        window.switchContent = function(myFile){
            $('.modal-body').load(myFile);
        };
    

    In the modal just put a link or a button

        <a href="Javascript: switchContent('myFile.html'); return false;">
         click here to load another file</a>
    

    If you just want to switch beetween 2 modals:

        window.switchModal = function(){
            $('#myModal-1').modal('hide');
            setTimeout(function(){ $('#myModal-2').modal(); }, 500);
            // the setTimeout avoid all problems with scrollbars
        };
    

    In the modal just put a link or a button

        <a href="Javascript: switchModal(); return false;">
         click here to switch to the second modal</a>
    
    0 讨论(0)
  • 2020-11-27 11:47

    My code works well using data-dismiss.

    <li class="step1">
        <a href="#" class="button-popup" data-dismiss="modal" data-toggle="modal" data-target="#lightbox1">
            <p class="text-label">Step 1</p>
            <p class="text-text">Plan your Regime</p>
        </a>
    
    </li>
    <li class="step2">
        <a href="#" class="button-popup" data-dismiss="modal" data-toggle="modal" data-target="#lightbox2">
            <p class="text-label">Step 2</p>
            <p class="text-text">Plan your menu</p>
        </a>
    </li>
    <li class="step3 active">
        <a href="#" class="button-popup" data-toggle="modal" data-dismiss="modal" data-target="#lightbox3">
            <p class="text-label">Step 3</p>
            <p class="text-text">This Step is Undone.</p>
        </a>
    </li>
    
    0 讨论(0)
  • 2020-11-27 11:48

    You can actually detect when the old modal closes by calling the hidden.bs.modal event:

        $('.yourButton').click(function(e){
            e.preventDefault();
    
            $('#yourFirstModal')
                .modal('hide')
                .on('hidden.bs.modal', function (e) {
                    $('#yourSecondModal').modal('show');
    
                    $(this).off('hidden.bs.modal'); // Remove the 'on' event binding
                });
    
        });
    

    For more info: http://getbootstrap.com/javascript/#modals-events

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

    For someone who use bootstrap 4 https://jsfiddle.net/helloroy/tmm9juoh/

    var modal_lv = 0;
    $('.modal').on('shown.bs.modal', function (e) {
        $('.modal-backdrop:last').css('zIndex',1051+modal_lv);
        $(e.currentTarget).css('zIndex',1052+modal_lv);
        modal_lv++
    });
    
    $('.modal').on('hidden.bs.modal', function (e) {
        modal_lv--
    });
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
    
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-a">
      Launch demo modal a
    </button>
    
    <div class="modal fade" id="modal-a" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-b">
      Launch another demo modal b
    </button>
    <p class="my-3">
    Not good for fade In.
    </p>
    <p class="my-3">
    who help to improve?
    </p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    
    <div class="modal fade" id="modal-b" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-c">
      Launch another demo modal c
    </button>
    <p class="my-3">
    But good enough for static modal
    </p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    
    
    <div class="modal" id="modal-c" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
    <p class="my-3">That's all.</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

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

    data-dismiss makes the current modal window force close

    data-toggle opens up a new modal with the href content inside it

    <a data-dismiss="modal" data-toggle="modal" href="#lost">Click</a>
    

    or

    <a data-dismiss="modal" onclick="call the new div here">Click</a>
    

    do let us know if it works.

    • You might also want to take a look around the Modal Documentation
    0 讨论(0)
提交回复
热议问题