Close Bootstrap Modal

2020-01-25 03:49发布

I have a bootstrap modal dialog box that I want to show initially, then when the user clicks on the page, it disappears. I have the following:

$(function () {
   $('#modal').modal(toggle)
});

 <div class="modal" id='modal'>
     <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Error:</h3>
        </div>
        <div class="modal-body">
        <p>Please correct the following errors:</p>
        </div>
     </div>
 </div>

The modal is displayed initially, but it does not close when clicked outside of the modal. Also, the content area is not greyed out.. How can I get the modal to display initially, then close after the user clicks outside the area? And how can I get the background to be grayed out as in the demo?

24条回答
倾城 Initia
2楼-- · 2020-01-25 03:52

Besides, you can "click" on a 'x', that closes dialog. E.g.:

$(".ui-dialog-titlebar-close").click();

查看更多
何必那么认真
3楼-- · 2020-01-25 03:52

some times the solution up doesn't work so you d have properly to remove the in class and add the css display:none manually .

$("#modal").removeClass("in");
$("#modal").css("display","none");
查看更多
ゆ 、 Hurt°
4楼-- · 2020-01-25 03:53

this worked for me:

<span class="button" data-dismiss="modal" aria-label="Close">cancel</span>

use this link modal close

查看更多
Anthone
5楼-- · 2020-01-25 03:56
$('#modal').modal('toggle'); 

or

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

should work.

But if nothing else works you can call the modal close button directly:

$("#modal .close").click()
查看更多
贪生不怕死
6楼-- · 2020-01-25 03:56

This works well

$(function () {
   $('#modal').modal('toggle');
});

However, when you have multiple modals stacking on top one another it is not effective, so instead , this works

data-dismiss="modal"
查看更多
The star\"
7楼-- · 2020-01-25 03:59

this one is pretty good and it also works in angular 2

$("#modal .close").click()
查看更多
登录 后发表回答