如果单击页面上的链接,如何防止页面无法导航。我有下面的代码,但它显示了两次消息。该页面还有一个数据网格。弹出窗口的消息仅在我通过单击网格上的记录离开页面时显示两次。其他任何地方都很好。
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;
});
});
答案 0 :(得分: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;
});
});