我有以下javaScript
编辑:包含cahngesSaved的分配
var changesSaved = true;
$(document).ready(function () {
$(".applyChanges").click(function (e) {
e.preventDefault();
var id = (this).id;
var dayofweek = document.getElementById("dayofweek" + id).value;
var hour = document.getElementById("hour" + id).value;
var duration = document.getElementById("duration" + id).value;
$.ajax({
url: '@Url.Action("ApplyChanges")',
type: 'POST',
data: {
id: id,
dayofweek: dayofweek,
hour: hour,
duration: duration
},
success: function (data) {
$('#Calendar').fullCalendar('refetchEvents')
$('#Calendar2').fullCalendar('refetchEvents')
changesSaved = false;
}
});
});
});
window.onbeforeunload = function () {
if (changesSaved == true) {
return true;
} else {
return ("You havent saved your changes. Are you sure you want to leave the page?")
}
}
但无论设置的changeSaved是什么,都会提示消息。
我实际得到的是以下内容:
或者如果changesSaved设置为false,则表示为false。
我可以不这样做吗?
答案 0 :(得分:2)
<body onbeforeunload="return bye()">
function bye(){
if(changesSaved) {
return "You havent saved your changes."
}
}
我就是这样做的。
或纯JavaScript:
window.onbeforeunload = function() {
if (changesSaved) {
return "You haven't saved your changes.";
}
};
答案 1 :(得分:0)
window.onbeforeunload = function () {
var changesSaved = confirm("You havent saved your changes. Are you sure you want to leave the page?");
if (changesSaved) {
return true;
} else {
return false;
}