【问题标题】:jQuery - Unable to get confirmation dialog to appear in FirefoxjQuery - 无法让确认对话框出现在 Firefox 中
【发布时间】: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


【解决方案1】:

编辑/警告:似乎我的解决方案在 Firefox 中不起作用,请参阅下面的第一条评论。

您不需要为此使用 confirm()。 只需在返回中输入一个字符串即可触发警报。

示例:

$(window).on('beforeunload', function() {
    var e = $.Event('webapp:page:closing');
    $(window).trigger(e); // let other modules determine whether to prevent closing
    if(e.isDefaultPrevented()) {
        // e.message is optional
        return e.message || 'You have unsaved stuff. Are you sure to leave?';
    }
});

更多信息在这里:http://hackab.it/2013/05/page-closing-confirm/

【讨论】:

猜你喜欢
  • 2014-08-28
  • 2010-10-27
  • 1970-01-01
  • 2016-10-08
  • 2011-05-24
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多