我有下面的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?")
}
}
但該消息被無論使用什麼changesSaved設置過的提示。
什麼其實我真正得到如下:
,或者如果changesSaved設置爲false,它說假的來代替。
我可以不這樣做嗎?
你在哪裏設置'changesSaved'? –
添加'console.log(changesSaved)'並查看函數內部的值。 –