最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Hide Modal in Twitter Bootstrap when clicked - Stack Overflow

matteradmin3PV0评论

Here is a sample code from twitter bootstrap but . . .

<div class="modal">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
        <p>One fine body…</p>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn">Close</a>
        <a href="#" class="btn btn-primary">Save changes</a>
    </div>
</div>

The scenario is this: I have a redirect page and I want to display the modal. Yes, of course I made it displayed but how do I close it? I tried different ways the click function but nothing happened.

Here is a sample code from twitter bootstrap but . . .

<div class="modal">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
        <p>One fine body…</p>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn">Close</a>
        <a href="#" class="btn btn-primary">Save changes</a>
    </div>
</div>

The scenario is this: I have a redirect page and I want to display the modal. Yes, of course I made it displayed but how do I close it? I tried different ways the click function but nothing happened.

Share Improve this question edited Jun 18, 2013 at 8:59 user2247326 asked Jun 18, 2013 at 7:58 user2247326user2247326 1732 gold badges3 silver badges12 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

From bootstrap doc :

Manually hides a modal.

.modal('hide')

$('.modal').modal('hide')

So if you want to hide it on click you can bind this action to the click event

$('#yourid').on('click' , function() { 
  $('.modal').modal('hide')
});

if you want to hide it after some second that you loaded the document you can try with a timeout

$(function(){  //document ready
..
.. //your code
..

 setTimeout(function(){
       $('.modal').modal('hide')
 }, 2000);  //2000 = 2 second

..
})

UPDATE 1

To bind it on the close button give him a class or id

<a href="#" class="btn closebtn">Close</a>

now

$('.closebtn').on('click',function() {
   $('.modal').modal('hide');
});

If you want to bind only on that close button use an id instead of a class

Old post but this can now be done using HTML attributes.

 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

try this

$('.modal').modal('hide');

I had the same problem. If it's still needed you can try to put your 'click' event in another event 'shown.bs.dropdown' like so:

$('#myDropdown').on('show.bs.dropdown', function () {
  $('#yourid').on('click' , function() { 
    $('#myDropdown').modal('hide');
  });
});

In my case the 'click' event wasn't binded when the modal was shown. So when I put it in 'show.bs.dropdown' event, everything went as I expected. 'click' was binded to modal as soon as modal was shown.

Post a comment

comment list (0)

  1. No comments so far