【发布时间】:2014-05-06 22:35:27
【问题描述】:
我正在尝试显示一个确认对话框以显示一条自定义消息,该消息将根据单击的按钮保存或忽略对表单的更改。
我使用 Firefox 制作的部分代码段不再有效。 在 beforeunload 事件处理程序中使用确认函数,我无法在 Firefox 中显示确认对话框,以防止用户在不接受更改或放弃更改的情况下更改页面。
是我的方法不正确还是浏览器有问题?
谢谢。
var confirmVal = false;
var firstConfirm = false;
$(window).bind('beforeunload', function () {
if (!firstConfirm) {
firstConfirm = true;
//This is where the confirm dialog should show
//It worked in firefox previously but not currently
//It still works in Internet Explorer
confirmVal = confirm("Save changes? \"OK\" saves changes. \"Cancel\" removes changes.");
}
if (confirmVal) {
// Perform update of database in this block with ajax
}
else
return;
});
【问题讨论】:
-
在 jsFiddle 中使用 FF 对我来说效果很好。你确定你有jquery吗? jsfiddle.net/sC38X
-
感谢您的链接。我必须在 jsfiddle 中“运行”代码才能看到对话框。我想确定代码在“离开”页面时是否有效。那是我的问题。但这让我确信我的代码正在触发确认。
-
好的,所以我尝试转到当前页面以外的新页面,然后出现对话框。我是否需要其他事件,例如重新加载以触发确认?
-
您不再能够从 beforeunload 处理程序内部发出警报/确认,因此这不再起作用。这已在 [bugzilla.mozilla.org/show_bug.cgi?id=588292]. 中“修复”
标签: javascript jquery