当前位置 : 首页 » 互动问答 » 正文

Disallow Twitter Bootstrap modal window from closing

分类 : 互动问答 | 发布时间 : 2012-03-28 01:10:49 | 评论 : 18 | 浏览 : 314152 | 喜欢 : 514

I am creating a modal window using Twitter Bootstrap. The default behavior is if you click outside the modal area, the modal will automatically close. I would like to disable that -- i.e. not close the modal window when clicking outside the modal.

Can someone share jQuery code to do this?

回答(18)

  • 1楼
  • I believe you want to set the backdrop value to static. If you want to avoid the window to close when using the Esc key, you have to set another value.

    Example:

    <a data-controls-modal="your_div_id"
       data-backdrop="static"
       data-keyboard="false"
       href="#">
    

    OR if you are using JavaScript:

    $('#myModal').modal({
      backdrop: 'static',
      keyboard: false
    });
    
  • 2楼
  • Just set the backdrop property to 'static'.

    $('#myModal').modal({
      backdrop: 'static',
      keyboard: true
    })
    

    You may also want to set the keyboard property to false because that prevents the modal from being closed by pressing the Esc key on the keyboard.

    $('#myModal').modal({
      backdrop: 'static',
      keyboard: false
    })
    

    myModal is the ID of the div that contains your modal content.

  • 3楼
  • You can also include these attributes in the modal definition itself:

    <div class="modal hide fade" data-keyboard="false" data-backdrop="static">
    
  • 4楼
  • If you already have initialized the modal window, then you may want to reset the options with $('#myModal').removeData("modal").modal({backdrop: 'static', keyboard: false}) to make sure it will apply the new options.

  • 5楼
  • Override the Bootstrap ‘hide’ event of Dialog and stop its default behavior (to dispose the dialog).

    Please see the below code snippet:

       $('#yourDialogID').on('hide.bs.modal', function(e) {
    
           e.preventDefault();
       });
    

    It works fine in our case.

  • 6楼
  • Yes, you can do it like this:

    <div id="myModal"  tabindex="-1" role="dialog"
         aria-labelledby="myModalLabel"
         aria-hidden="true"
         data-backdrop="static"  data-keyboard="false">
    
  • 7楼
  • Kind of like @AymKdn's answer, but this will allow you to change the options without re-initializing the modal.

    $('#myModal').data('modal').options.keyboard = false;
    

    Or if you need to do multiple options, JavaScript's with comes in handy here!

    with ($('#myModal').data("modal").options) {
        backdrop = 'static';
        keyboard = false;
    }
    

    If the modal is already open, these options will only take effect the next time the modal is opened.

  • 8楼
  • Just add these two things

    data-backdrop="static" 
    data-keyboard="false"
    

    It will look like this now

    <div class="modal fade bs-example-modal-sm" id="myModal" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    

    It will disable the escape button and also the click anywhere and hide.

  • 9楼
  • You can disable the background's click-to-close behavior and make this the default for all your modals by adding this JavaScript to your page (make sure it is executed after jQuery and Bootstrap JS are loaded):

    $(function() {
        $.fn.modal.Constructor.DEFAULTS.backdrop = 'static';
    });
    
  • 10楼
  • As D3VELOPER says, the following code resolve it:

    $('#modal').removeData('bs.modal').modal({backdrop: 'static', keyboard: false});
    

    I'm using both jquery & bootstrap and simply removeData('modal') don't work.

  • 11楼
  • The best I found is add this code to the link

    <!-- Link -->
    <a href="#mdl" role="button"  data-backdrop="static" data-keyboard="false" data-toggle="modal" id_team="" ></a>
    <-- Div -->
    <div id="mdl" class="modal hide fade" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static"></div>
    
  • 12楼
  • In case anyone comes here from Google trying to figure out how to prevent someone from closing a modal, don't forget that there's also a close button on the top right of the modal that needs to be removed.

    I used some CSS to hide it:

    #Modal .modal-header button.close {
        visibility: hidden;
    }
    

    Note that using "display: none;" gets overwritten when the modal is created, so don't use that.

  • 13楼
  • Doing that is very easy nowadays. Just add:

    data-backdrop="static" data-keyboard="false" 
    

    In your modal divider.

  • 14楼
  • If you want to conditionally disable the backdrop click closing feature. You can use the following line to set the backdrop option to static during runtime.

    Bootstrap v3.xx

    jQuery('#MyModal').data('bs.modal').options.backdrop = 'static';
    

    Bootstrap v2.xx

    jQuery('#MyModal').data('modal').options.backdrop = 'static';
    

    This will prevent an already instantiated model with backdrop option set to false (the default behavior), from closing.

  • 15楼
  • You can set default behavior of modal popup by using of below line of code:

     $.fn.modal.prototype.constructor.Constructor.DEFAULTS.backdrop = 'static';
    
  • 16楼
  • Well, this is another solution that some of you guys might be looking for (as I was..)

    My problem was similar, the modal box was closing while the iframe I had inside was loading, so I had to disable the modal dismiss until the Iframe finishes loading, then re-enable.

    The solutions presented here were not working 100%.

    My solution was this:

    showLocationModal = function(loc){
    
        var is_loading = true;
    
        if(is_loading === true) {
    
            is_loading  = false;
            var $modal   = $('#locationModal');
    
            $modal.modal({show:true});
    
            // prevent Modal to close before the iframe is loaded
            $modal.on("hide", function (e) {
                if(is_loading !== true) {
                    e.preventDefault();
                    return false
                }
            });
    
            // populate Modal
            $modal.find('.modal-body iframe').hide().attr('src', location.link).load(function(){
    
                is_loading = true;
         });
    }};
    

    So I temporarily prevent the Modal from closing with:

    $modal.on("hide", function (e) {
        if(is_loading !== true) {
            e.preventDefault();
            return false
        }
    });
    

    But ith the var is_loading that will re enable closing after the Iframe has loaded.

  • 17楼
  • <button type="button" class="btn btn-info btn-md" id="myBtn3">Static 
    Modal</button>
    
    <!-- Modal -->
    <div class="modal fade" id="myModal3" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">×</button>
          <h4 class="modal-title">Static Backdrop</h4>
        </div>
        <div class="modal-body">
          <p>You cannot click outside of this modal to close it.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-
          dismiss="modal">Close</button>
        </div>
       </div>
      </div>
    </div>
       <script>
        $("#myBtn3").click(function(){
         $("#myModal3").modal({backdrop: "static"});
        });
       });
      </script>
    

相关阅读:

Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

jQuery event to trigger action when a div is made visible

Check if user is using IE with jQuery

How to get a responsive button in bootstrap 3

Change the image source on rollover using jQuery

How to get the data-id attribute?

jQuery .scrollTop(); + animation

wait() or sleep() function in jquery?

jQuery "blinking highlight" effect on div?

Set select option 'selected', by value