Javascript Onbeforeunload异常
我尝试对onbeforeunload做一个例外,并在数量不同于零时警告不要丢失数据: 我试过这个:Javascript Onbeforeunload异常,javascript,jquery,ajax,Javascript,Jquery,Ajax,我尝试对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
<!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>
$(window).bind('beforeunload',函数(e){
var prompt=true;
$('input.qty')。每个(函数(i,输入){
如果($(输入).val()!='0'){
prompt=true;
}
});
如果(提示){
returne.returnValue='Some warning message';
}
});
但它不像我想要的那样工作
有人能帮忙吗
非常感谢。提示符总是正确的:默认情况下,当某些内容与零不同时
默认情况下将其设置为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';
}
@Bizboss:它是如何工作的,你希望它做什么?通过你的编辑,我可以在单击链接时退出页面,这不会让用户直接退出页面,脚本必须显示警告消息,当一个或多个文本字段中的数量不同于零时。。。我希望现在更清楚了。非常感谢:)@Bizboss:这把小提琴对我来说很好:。这不是你要找的吗?