关闭/隐藏模式时页面不刷新

时间:2019-07-26 15:49:18

标签: javascript jquery bootstrap-4 bootstrap-modal

我有一个在单击按钮后加载的模态。我想要的是,只要该模式被隐藏,就刷新页面。但它似乎无法正常工作,也看不到我的错误。我的代码有什么问题?

HTML:

<!DOCTYPE html>
<head>
    <!--Libreries for modal -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

</head>
<body>

    <button id="btn" onclick="openM()">Click</button>

    <div class="modal fade" id="ModalMSJ" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" style="font-weight: bold; color:black;" id="exampleModalLabel">Modal</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" style="color:black;" id="MSJ">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>                 
</body>

JS:

//refresh page when modal is hidden
$('#ModalMSJ').on('hide.bs.modal','.modal', function () {
    window.open('ModalP.php', '_self'); 
}); 

//open modal on button click
function openM() {
    $("#ModalMSJ").modal("show"); 
}

2 个答案:

答案 0 :(得分:1)

您可以尝试更换:

window.open('ModalP.php', '_self');

使用

location.reload();
//or
document.location.reload();

答案 1 :(得分:1)

您的事件绑定错误,根本没有调用hide回调。请删除第二个参数“ .modal”,因为它不需要。最终结果:

$('#ModalMSJ').on('hide.bs.modal', function () 
{   
    window.open('ModalP.php', '_self'); 
});