Bootstrap modal: close current, open new

前端 未结 27 1665
离开以前
离开以前 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 00:55

    I know this is a late answer, but it might be useful. Here's a proper and clean way to get this done, as @karima's mentioned above. You can actually fire two functions at once; trigger and dismiss the modal.

    HTML Markup;

    <!-- Button trigger modal -->
    <ANY data-toggle="modal" data-target="TARGET">...</ANY>
    
    <div class="modal fade" id="SELECTOR" tabindex="-1">
      <div class="modal-dialog">
       <div class="modal-content">
        <div class="modal-body"> ... </div>
         <div class="modal-footer">           <!-- ↓ -->  <!--      ↓      -->
          <ANY data-toggle="modal" data-target="TARGET-2" data-dismiss="modal">...</ANY>
         </div>
       </div>
      </div>
    </div>
    

    Demo

    0 讨论(0)
  • 2020-11-29 00:55

    By using the following code you can hide first modal and immediately open second modal, by using same strategy you can hide second modal and show the first one.

    $("#buttonId").on("click", function(){
        $("#currentModalId").modal("hide");
        $("#currentModalId").on("hidden.bs.modal",function(){
        $("#newModalId").modal("show");
        });
    });
    
    0 讨论(0)
  • 2020-11-29 00:56
    data-dismiss="modal" 
    

    it is used to close the existing opened model. you can set it to the new model

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

    In first modal:

    replace modal dismiss link in footer with close link below.

    <div class="modal-footer">
          <a href="#"  data-dismiss="modal" class="btn btn-primary" data-toggle="modal" data-target="#second_model_id">Close</a>
    </div>
    

    In second modal:

    Then second modal replace top div with below div tag.

    <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="add text for disabled people here" aria-hidden="true" id="second_model_id">
    
    0 讨论(0)
  • 2020-11-29 00:59

    Had the same issue, writing this here in case someone in the future stumbles upon this and has issues with multiple modals that have to have scrolling as well (I'm using Bootstrap 3.3.7)

    What I did is have a button like this inside my first modal:

    <div id="FirstId" data-dismiss="modal" data-toggle="modal" data-target="#YourModalId_2">Open modal</div>
    

    It will hide the first and then display the second, and in the second modal I would have a close button that would look like this:

    <div id="SecondId" data-dismiss="modal" data-toggle="modal" data-target="#YourModalId_1">Close</div>
    

    So this will close the second modal and open up the first one and to make scrolling work I added to my .css file this line:

    .modal {
    overflow: auto !important;
    }
    

    PS: Just a side note, you have to have these modals separately, the minor modal can not be nested in the first one as you hide the first one

    So here's the full example based on the bootstrap modal example:

    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Launch demo modal
    </button>
    
    <!-- Modal 1 -->
    <div class="modal fade" id="exampleModal" 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">
            Add lorem ipsum here
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal" data-toggle="modal" data-target="#exampleModal2">
              Open second modal
            </button>
          </div>
        </div>
      </div>
    </div>
    
    <!-- Modal 2 -->
    <div class="modal fade" id="exampleModal2" 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">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal"  data-toggle="modal" data-target="#exampleModal">Close</button>
          </div>
        </div>
      </div>
    </div>
    
    0 讨论(0)
  • 2020-11-29 01:00

    Problem with data-dismiss="modal" is it will shift your content to left

    I am sharing what worked for me, problem statment was opening pop1 from pop2

    JS CODE

    var showPopup2 = false;
    $('#popup1').on('hidden.bs.modal', function () {
        if (showPopup2) {
            $('#popup2').modal('show');
            showPopup2 = false;
        }
    });
    
    $("#popup2").click(function() {
        $('#popup1').modal('hide');
        showPopup2 = true;
    });
    
    0 讨论(0)
提交回复
热议问题