gpt4 book ai didi

Javascript/jQuery 返回 false 并不停止执行

转载 作者:行者123 更新时间:2023-11-28 13:52:35 25 4
gpt4 key购买 nike

我有一个代码块,它在允许提交表单之前检查某些元素,循环循环正常。

我预计,或者至少希望,第一次发现问题时,会显示警报,然后停止执行。实际发生的情况是警报显示多次,一直到最后一个,并且只有最后一个实际上返回 false!

我不明白这是为什么,我做错了什么? JavaScript 如下。

$('#deliverInForm').submit(function(){
$('.childRow').each(function(){
if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()==''){
alert('You have entered a quantity but no Bin, please correct and try again!');
return false;
}else if($(this).find('.unitQty').val()==''){
alert('You have entered a Bin but no quantity, please correct and try again!');
return false;
}else if($(this).find('.unitQty').val()==0){
alert('Can\'t move zero units into a bay, please correct and try again!');
return false;
}else if($(this).find('.unitQty').val()<0){
alert('Can\'t deliver in minus units into a bay, please correct and try again!');
return false;
}
}
});

if($('#supDocNo').val()==''){
alert('Can\'t leave Supplier Reference blank, please correct and try again!');
return false;
}
return true;
});

最佳答案

让你的函数接受一个事件参数,然后调用 event.preventDefault()

例如

$('#deliverInForm').submit(function(event){
$('.childRow').each(function(){
if($(this).find('.thisBinName').val()!='' || $(this).find('.unitQty').val()!=''){
if($(this).find('.thisBinName').val()==''){
alert('You have entered a quantity but no Bin, please correct and try again!');
event.preventDefault();
return false;
}else if($(this).find('.unitQty').val()==''){
alert('You have entered a Bin but no quantity, please correct and try again!');
event.preventDefault();
return false;
}else if($(this).find('.unitQty').val()==0){
alert('Can\'t move zero units into a bay, please correct and try again!');
event.preventDefault();
return false;
}else if($(this).find('.unitQty').val()<0){
alert('Can\'t deliver in minus units into a bay, please correct and try again!');
event.preventDefault();
return false;
}
}
});

if($('#supDocNo').val()==''){
alert('Can\'t leave Supplier Reference blank, please correct and try again!');
event.preventDefault();
return false;
}
return true;
});

关于Javascript/jQuery 返回 false 并不停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9993847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com