我有以下 JavaScript
编辑:包括更改已保存的分配
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?")
}
}
但无论changesSaved
设置什么,都会提示消息。
实际上我实际得到的是以下内容:
或者如果changesSaved
设置为 false,则改为 false。
我不能这样做吗?