如何防止“您确定要离开此页面吗?”提交表单提醒? [英] How to prevent "Are you sure you want to navigate away from this page?" alert on form submission?

查看:364
本文介绍了如何防止“您确定要离开此页面吗?”提交表单提醒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的html表单,其中有几个字段。

我想确保用户在试图离开页面时收到警告消息。所以我在页面上添加了脚本,当用户试图离开这个页面时,脚本就会运行。

但是,当用户尝试提交表单时,我也会收到此警报消息。当用户保存表单时,我不需要此警报消息。



请帮忙。

以下是一些示例代码。

 < html> 
< head>

< script>
函数verifyexit(){return'你确定吗?'; } window.onbeforeunload = verifyexit;
< / script>
< / head>
< body>
前往< a href =b.html>另一个页面< / a>
< form action =c.html>
名称:< input type =textname =namevalue =/> <峰; br>
电子邮件地址:< input type =textname =emailvalue =/> <峰; br>
< input type =submitvalue =save/>
< / form>
< / body>


解决方案



<$> p $ p> window.onbeforeunload =你确定要离开吗?;

要关闭它:

  window.onbeforeunload = null; 

请记住,这不是一个正常的事件 - 您无法在标准方式。

检查值?这取决于您的验证框架。



在jQuery中,这可能类似于(非常基本的示例):

<$ p ($(this).val()!=)
window.onbeforeunload =$($)你确定要离开吗?;
});

使用JQuery 这个东西很容易做到。因为你可以绑定设置。



它不足以执行onbeforeunload,如果有人开始编辑东西。



要在Chrome和Safari中使用此功能,您必须像这样做

  window.onbeforeunload = function(e){
e = e || window.event;

var msg =确定你想离开?;

//对于版本4之前的IE和Firefox
if(e){
e.returnValue = msg;
}

//对于Safari
return msg;
};




这是参考的一般规则

  window.onbeforeunload = function(e){
e = e || window.event;

//对于版本4之前的IE< 8和Firefox
if(e){
e.returnValue ='Any string';
}

//对于Chrome,Safari,IE8 +和Opera 12+
返回'Any string';
};

参考资料:https://developer.mozilla.org/en/DOM/window.onbeforeunload


I have a simple html form with few fields in it.

I wanted to make sure that user gets an alert message when he is trying to leave the page. So I added the script on the page which will run whenever user is trying to navigate away from this page.

But I am also getting this alert message when user is trying to submit the form. I dont want this alert message when user is saving the form.

Please help.

Below is some sample code.

<html>
    <head>

    <script>
        function verifyexit() { return 'Are you sure ?'; }           window.onbeforeunload = verifyexit;
    </script>       
</head>
<body>
        Go to <a href="b.html"> another page </a> 
        <form action="c.html">
            name : <input type="text" name="name" value=""/> <br>
            email : <input type="text" name="email" value=""/> <br>
            <input type="submit" value="save"/>
        </form>
</body> 

解决方案

stackoverflow reference

To turn it on:

window.onbeforeunload = "Are you sure you want to leave?";

To turn it off:

window.onbeforeunload = null;

Bear in mind that this isn't a normal event - you can't bind to it in the standard way.

To check for values? That depends on your validation framework.

In jQuery this could be something like (very basic example):

$('input').change(function() {
    if( $(this).val() != "" )
        window.onbeforeunload = "Are you sure you want to leave?";
});

With JQuery this stuff is pretty easy to do. Since you can bind to sets.

Its NOT enough to do the onbeforeunload, you want to only trigger the navigate away if someone started editing stuff.

To make this work in Chrome and Safari, you would have to do it like this

window.onbeforeunload = function (e) {
    e = e || window.event;

    var msg = "Sure you want to leave?";

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue = msg;
    }

    // For Safari
    return msg;
};


This is the general rule in reference

window.onbeforeunload = function (e) {
  e = e || window.event;

  // For IE<8 and Firefox prior to version 4
  if (e) {
    e.returnValue = 'Any string';
  }

  // For Chrome, Safari, IE8+ and Opera 12+
  return 'Any string';
};

reference: https://developer.mozilla.org/en/DOM/window.onbeforeunload

这篇关于如何防止“您确定要离开此页面吗?”提交表单提醒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆