我有以下 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
也被设定了。
我实际上得到的是以下内容:
or if changesSaved
设置为 false 它会显示 false。
我可以不这样做吗?
<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.";
}
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)