Reload content in modal (twitter bootstrap)

前端 未结 14 1684
青春惊慌失措
青春惊慌失措 2020-12-02 04:38

I\'m using twitter bootstrap\'s modal popup.


      
      
14条回答
  • 2020-12-02 05:20

    You can force Modal to refresh the popup by adding this line at the end of the hide method of the Modal plugin (If you are using bootstrap-transition.js v2.1.1, it should be at line 836)

    this.$element.removeData()
    

    Or with an event listener

    $('#modal').on('hidden', function() {
        $(this).data('modal').$element.removeData();
    })
    
    0 讨论(0)
  • 2020-12-02 05:21

    You can try this:

    $('#modal').on('hidden.bs.modal', function() {
        $(this).removeData('bs.modal');
    });
    
    0 讨论(0)
  • 2020-12-02 05:21

    step 1 : Create a wrapper for the modal called clone-modal-wrapper.

    step 2 : Create a blank div called modal-wrapper.

    Step 3 : Copy the modal element from clone-modal-wrapper to modal-wrapper.

    step 4 : Toggle the modal of modal-wrapper.

    <a data-toggle="modal" class='my-modal'>Open modal</a>
    
    <div class="clone-modal-wrapper">
       <div class='my-modal' class="modal fade">
          <div class="modal-dialog">
             <div class="modal-content">
                <div class="modal-header">
                   <a class="close" data-dismiss="modal">×</a>
                   <h3>Header</h3>
                </div>
                <div class="modal-body"></div>
                <div class="modal-footer">
                   <input type="submit" class="btn btn-success" value="Save"/>
                </div>
             </div>
          </div>
       </div>
    </div>
    
    <div class="modal-wrapper"></div>
    
    
    $("a[data-target=#myModal]").click(function (e) {
        e.preventDefault();
        $(".modal-wrapper").html($(".clone-modal-wrapper").html());
        $('.modal-wrapper').find('.my-modal').modal('toggle');
    });
    
    0 讨论(0)
  • 2020-12-02 05:24

    To unload the data when the modal is closed you can use this with Bootstrap 2.x:

    $('#myModal').on('hidden', function() {
        $(this).removeData('modal');
    });
    

    And in Bootstrap 3 (https://github.com/twbs/bootstrap/pull/7935#issuecomment-18513516):

    $(document.body).on('hidden.bs.modal', function () {
        $('#myModal').removeData('bs.modal')
    });
    
    //Edit SL: more universal
    $(document).on('hidden.bs.modal', function (e) {
        $(e.target).removeData('bs.modal');
    });
    
    0 讨论(0)
  • 2020-12-02 05:27

    It will works for all version of twitterbootstrap

    Javascript code :

    <script type="text/javascript">
    /* <![CDATA[ */
    (function(){
       var bsModal = null;
       $("[data-toggle=modal]").click(function(e) {
          e.preventDefault();
          var trgId = $(this).attr('data-target'); 
          if ( bsModal == null ) 
           bsModal = $(trgId).modal;
          $.fn.bsModal = bsModal;
          $(trgId + " .modal-body").load($(this).attr("href"));
          $(trgId).bsModal('show');
        });
     })();
    /* <![CDATA[ */
    </script>
    

    links to modal are

    <a data-toggle="modal" data-target="#myModal" href="edit1.aspx">Open modal 1</a>
    <a data-toggle="modal" data-target="#myModal" href="edit2.aspx">Open modal 2</a>
    <a data-toggle="modal" data-target="#myModal" href="edit3.aspx">Open modal 3</a>
    

    pop up modal

    <div id="myModal" class="modal hide fade in">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <h3>Header</h3>
    </div>
    <div class="modal-body"></div>
    <div class="modal-footer">
        <input type="submit" class="btn btn-success" value="Save"/>
    </div>
    

    0 讨论(0)
  • 2020-12-02 05:28

    I am having the same problem, and I guess the way of doing this will be to remove the data-toggle attribute and have a custom handler for the links.

    Something in the lines of:

    $("a[data-target=#myModal]").click(function(ev) {
        ev.preventDefault();
        var target = $(this).attr("href");
    
        // load the url and show modal on success
        $("#myModal .modal-body").load(target, function() { 
             $("#myModal").modal("show"); 
        });
    });
    

    Will try it later and post comments.

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