0

当单击页面上的链接时,如何防止页面不导航。我有下面的代码,但它反而显示了两次消息。该页面还有一个数据网格。只有当我通过单击网格上的记录离开页面时,才会显示两次弹出消息。其他地方都很好。

   var canShowWarning = "@(Model.CanEdit)";
var showWarning = false;

window.onbeforeunload = function () {
    if (canShowWarning && showWarning) {
        return 'You have made unsaved changes';
    }
}
$(function () {
$("#Form").submit(function () {
        showWarning = false;
    });

    $("#Form").change(function () {
        showWarning = true;
    });
});
4

1 回答 1

0
var canShowWarning = "@(Model.CanEdit)";
var showWarning = false;
var alreadyShown = false;

window.onbeforeunload = function () {
    if (canShowWarning && showWarning && !alreadyShown) {
        alreadyShown = true;
        return 'You have made unsaved changes';
    }
}
$(function () {
$("#Form").submit(function () {
        showWarning = false;
    });

    $("#Form").change(function () {
        showWarning = true;
        alreadyShown =  false;
    });
});
于 2013-02-20T13:17:40.260 回答