我需要一些幫助!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