【问题标题】:How to prevent page unloading with modal window如何防止使用模态窗口卸载页面
【发布时间】:2015-01-21 17:34:59
【问题描述】:

我想检查页面中的任何文本区域是否已更改,如果是,如果用户在未提交的情况下导航离开,则停止导航并提示模式窗口(而不是来自 onbeforeunload 的模式窗口)以检查用户是否想要真正离开页面或留下并提交数据。

到目前为止,我已经做到了:

var needToConfirm = false;

$("input,textarea").on("input", function () {
    needToConfirm = true;
});

$("select").change(function () {
    needToConfirm = true;
});
function closeEditorWarning() {

if (needToConfirm) {
        var t = News;
        t.config.modalTitle.html("If you exit this page, your unsaved changes will be lost"), t.config.modalMsg.text("Are you sure you don't want to submit the changes?"), t.config.modalWindow.show(), t.config.modalAcceptBtn.on(click, null)
        return "null";
    }

}

window.onbeforeunload = closeEditorWarning();

这样,我得到了模态弹出窗口,但随后页面被卸载到用户立即选择的页面,而忽略了模态窗口。那么如何防止页面被卸载并让它等待用户确认他想离开而不提交更改?

【问题讨论】:

标签: jquery modal-dialog


【解决方案1】:

这是不可能的。中断页面卸载的唯一方法是在 onbeforeunload 处理程序 (like this question) 中返回 String 消息。

如果您要求的是什么,那么页面可能会无限期地保持打开状态,尽管用户请求关闭它们(显然,这不是好的行为)。

【讨论】:

  • 那么还有其他可以使用的处理程序吗?或其他方式来防止/暂停卸载?我在那里使用了 onbeforeunloa,因为这是我目前知道的唯一方法。但我想用模态窗口之一替换它显示的窗口,因为这是用于站点周围其他警告或消息的窗口
  • 我明白你想要做什么。这是不可能的。您没有在任何网站上看到您所描述的行为是有原因的。他们都使用本机警报,因为这是唯一的方法,而且永远不会改变。对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
相关资源
最近更新 更多