2

我有以下 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。

我不能这样做吗?

4

2 回答 2

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.";
    }
};
于 2012-06-03T20:09:42.357 回答
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;
    }
于 2012-06-03T20:10:04.563 回答