且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用jQuery编辑表单时确认离开页面

更新时间:2023-12-01 20:23:28

您可以使用 window.onbeforeunload 像这样:

You can do this using window.onbeforeunload like this:

window.onbeforeunload = function() {
    return 'Are you sure you want to navigate away from this page?';
};

因此,在您的代码中,应将其放在$.ajax调用的success回调中.

So in your code, you would put that inside the success callback of your $.ajax call.

要取消确认,您可以执行以下操作:

To unset the confirmation, you can do this:

window.onbeforeunload = null;

此外,请参见以下答案:如何覆盖OnBeforeUnload对话框并用我自己的对话框替换?

Also, see this answer: How can I override the OnBeforeUnload dialog and replace it with my own?