开发者

Onbeforeunload exception

I try to make an exception to onbeforeunload and a warning against loosing data when there is quantity different from zero:

I have tried this :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>

<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>

<body>
 <form>   
<p><input type="text" value="0" maxlength="12" id="qty" class="input-text qty" name="qty2603"></p>
<p><input type="text" value="0" maxlength="12" id="qty" class="input-text qty" name="qty2613"></p>
<p><input type=开发者_运维百科"text" value="0" maxlength="12" id="qty" class="input-text qty" name="qty2606"></p>


<p><a href="http://www.google.com">Google</a></p>
<p>
  <input type="submit" name="Submit" value="Envoyer" onclick="prompt = false;" />


</p>
 </form>

<script type="text/javascript">
$(window).bind('beforeunload', function(e) {
    var prompt = true;
    $('input.qty').each(function(i, input) {
      if ($(input).val() != '0') {
          prompt = true;
      }
    });

    if (prompt) {
        return e.returnValue = 'Some warning message';
    }
});
</script>

</body>
</html>

but it don't work like I want...

can anyone help ?

Thanks a lot.


prompt is always true: by default, and when something is different from zero.

Set it to false by default instead, and change to true when appropriate:

var prompt = false; // set to false by default

$('input.qty').each(function(i, input) {
  if ($(input).val() != '0') {
      prompt = true;
  }
});

if (prompt) {
    return e.returnValue = 'Some warning message';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜