Javascript jquery-单击添加异常
我有以下输入字段:Javascript jquery-单击添加异常,javascript,jquery,Javascript,Jquery,我有以下输入字段: <input type="text" value="5" maxlength="12" id="qty" class="input-text qty" name="qty2050" tabindex="1"> 及 我使用此代码防止用户丢失插入的数据(在退出页面时): window.onbeforeunload=函数(){ var showart=false; jQuery('.input text.qty')。每个(函数(){ //控制台日志(“功能1
<input type="text" value="5" maxlength="12" id="qty" class="input-text qty" name="qty2050" tabindex="1">
及
我使用此代码防止用户丢失插入的数据(在退出页面时):
window.onbeforeunload=函数(){
var showart=false;
jQuery('.input text.qty')。每个(函数(){
//控制台日志(“功能1”);
var val=parseInt(jQuery(this).val(),10);
如果(val>0){showAlert=true;}
//警报(val);
});
//控制台日志(“功能2”);
//console.log(showarert);
如果(showAlert==true){
控制台日志(“功能3”);
返回“您有未保存的更改!”;
}
}
我想在我的提交按钮中添加一个例外,并且当我的一个输入字段中的数量>0时,不显示警报消息
我的问题是添加此异常
谢谢您的帮助。这就是您想要的:
<SCRIPT>
function checkForm(e) {
if (!(window.confirm("Do you want to submit the form?")))
e.returnValue = false;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm" action="0801.html"
onSubmit = "return checkForm(event)">
<INPUT type=submit>
</FORM>
函数校验表(e){
如果(!(window.confirm(“是否要提交表单?”))
e、 returnValue=false;
}
您的检查将进入checkForm方法。在这种情况下,您可以使用bool confirmUnload。就像这里:
var confirmUnload=true;
$(“提交”)。单击(函数(){confirmUnload=false;});
window.onbeforeunload=函数(){
如果(确认卸载)
{
var showart=false;
jQuery('.input text.qty')。每个(函数(){
//控制台日志(“功能1”);
var val=parseInt(jQuery(this).val(),10);
如果(val>0){showAlert=true;}
//警报(val);
});
//控制台日志(“功能2”);
//console.log(showarert);
如果(showAlert==true){
控制台日志(“功能3”);
返回“您有未保存的更改!”;
}
}
}
也许可以提供更多关于您需要帮助的信息?我已经编辑了我的问题,我希望现在它更清楚了…数量>0是什么意思,您在说什么警报,也许可以尝试澄清您的问题…数量>0表示用户输入了数量,在这种情况下,它大于0;这个测试很简单,但是我如何调整代码来实现相同的功能呢?我还处于jquery开发的开始阶段。。。非常感谢,但在这一部分中,似乎代码$(“提交”).click(function(){confirmundload=false;})代码>未运行,我还添加了confirmUnload=false代码>直接指向我的按钮(onclick),但它也不起作用:(没关系,我已经将它添加到我的表单(onsubmit)中,现在可以了。非常感谢您的帮助:)
<script>
window.onbeforeunload = function() {
var showAlert = false;
jQuery('.input-text.qty').each(function (){
//console.log("fonction 1");
var val = parseInt(jQuery(this).val(),10);
if(val > 0) { showAlert = true; }
//alert(val);
});
//console.log("fonction 2");
//console.log(showAlert);
if (showAlert == true) {
console.log("fonction 3");
return 'You have unsaved changes!';
}
}
</script>
<SCRIPT>
function checkForm(e) {
if (!(window.confirm("Do you want to submit the form?")))
e.returnValue = false;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="theForm" action="0801.html"
onSubmit = "return checkForm(event)">
<INPUT type=submit>
</FORM>
<script>
var confirmUnload = true;
$("submit").click(function(){ confirmUnload = false; });
window.onbeforeunload = function() {
if (confirmUnload)
{
var showAlert = false;
jQuery('.input-text.qty').each(function (){
//console.log("fonction 1");
var val = parseInt(jQuery(this).val(),10);
if(val > 0) { showAlert = true; }
//alert(val);
});
//console.log("fonction 2");
//console.log(showAlert);
if (showAlert == true) {
console.log("fonction 3");
return 'You have unsaved changes!';
}
}
}
</script>