我尝试对onbeforeunload进行异常处理,并在数量不为零的情况下警告不要丢失数据:
我已经尝试过了:
<!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>但它并不像我想的那样工作...
有人能帮上忙吗?
非常感谢。
发布于 2011-07-29 20:08:12
prompt始终为真:默认情况下,以及当某些值不为零时。
默认情况下将其设置为false,并在适当的时候更改为true:
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';
}https://stackoverflow.com/questions/6872762
复制相似问题