我需要一些帮助!Javascript/jQuery返回false不会停止执行
我有一个代码块,允许表单提交前检查某些元素,循环通过OK。
我会预料到,或者至少希望第一次发现问题时,显示警告,然后停止执行。实际发生的情况是警报会多次显示,直到最后一个,而最后一个实际返回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;
});
这个方法的问题是,最终的检查('如果($('#supDoc ...')仍然会执行,可能导致多个警报。 – sje397 2012-04-03 13:14:18
简单解决e,而不是警告错误,将警报消息放入变量中,检查变量是否为空,如果不是,则警告其内容。或者在foreach之前放置第一个提醒;) – 2012-04-03 13:17:47