使用onbeforeunload创建自定义确认警告?

时间:2015-06-10 18:00:36

标签: javascript jquery alert onbeforeunload

我正在尝试为网站创建自定义提醒功能。它检查某些表单字段中是否有任何内容,如果存在,则在您尝试导航时应显示确认消息。 (不包括提交和取消按钮。)

我有这个代码来检查数据,导航不包括某些按钮:

 var leave_page_confirm = true;

                        function save_data_check() {
                            var msg;
                            if (leave_page_confirm === true) {
                                $('.input-right').each(function() {
                                    if (this.value) {
                                        leave_page_alert();
                                    }
                                });
                            }
                        }

window.onbeforeunload = save_data_check;

我希望排除被视为导航离开页面的按钮将包含:

onclick="leave_page_confirm=false;"

leave_page_alert函数的定义如下:

var leave_page_alert = function() {

  $("#custom-alert-overlay").show();

}

在不知道CSS和HTML的情况下,只需说“custom-alert-overlay”是包含按钮的元素的id。

我难以理解的是要添加到“leave_page_alert()”的内容,以防止在点击其中一个按钮之前加载页面。

为了论证,假设“custom-alert-overlay”包含以下按钮:

<button type="button" id="stay">Stay on this Page</button>
<button type="button" id="leave">Leave this Page</button>

尽管谷歌搜索,我还没有设法弄清楚一个人会怎么做......

0 个答案:

没有答案