Bootstrap: 弹出窗口Modal不消失, 点击外部不关闭窗口Modal, How to prevent Bootstrap modal from closing when clicking outside

 

使用Modal的backdrop选项

默认情况下,如果单击Bootstrap模式窗口外部,即在背景或暗区域,它将关闭并消失。当你在模态内并按下键盘上的退出键时也会发生这种情况。但是你可以通过将modal的backdrop选项设置为statickeyboard选项来防止这种情况发生false,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Disallow Bootstrap Modal from Closing</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $(".show-modal").click(function(){
        $("#myModal").modal({
            backdrop: 'static',
            keyboard: false
        });
    });
});
</script>
</head>
<body>
    <!-- Button HTML (to Trigger Modal) -->
    <input type="button" class="btn btn-lg btn-primary show-modal" value="Show Demo Modal">
    
    <!-- Modal HTML -->
    <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Confirmation</h4>
                </div>
                <div class="modal-body">
                    <p>Do you want to save changes you made to document before closing?</p>
                    <p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

 

本文: Bootstrap: 弹出窗口不消失, 点击外部不关闭窗口, How to prevent Bootstrap modal from closing when clicking outside

Leave a Reply