Jquery的each里面用return false代替break; return ture 代替continue

function methodone(){ 
.... 
   $.each(array,
function(){ 
   
if(条件成立){ 
     
return true
   } 
  }); 
.... 

在一个function里有一个each,在each里某种条件 成立的话,就把这个function返回true或者false

但是在each代码块内不能使用break和continue,要实现break和continue的功能的话,要使用其它的方式
break----用return false;
continue --用return ture;

所以当我在each里想使用return true给这个function返回时,其实只是让each继续执行而以
连each都没有中断,所以function也就不能return了 。

解决办法:通过try捕捉throw出来的错误,达到退出each、并返回错误的目标!

function CheckBatchRow(obj) {
    
if ($(":checkbox[id$='chkSelect']:checked").size() > 0) {
        
try {
            $(
":checkbox[id$='chkSelect']:checked").each(function() {
                
var prefix = this.id.replace("chkSelect""");

                
var txtDateStart = $("#" + prefix + "txtDateStart");
                
var txtDateEnd = $("#" + prefix + "txtDateEnd");
                
if ($.trim(txtDateStart.val()) == '' || $.trim(txtDateEnd.val()) == '') {
                    txtDateStart.addClass(
"fareValidForm");
                    txtDateEnd.addClass(
"fareValidForm");
                    
throw "对不起,请您填写有效期!";
 
                }
                
else {
                    d1Arr 
= txtDateStart.val().split('-');
                    d2Arr 
= txtDateEnd.val().split('-');
                    v1 
= new Date(d1Arr[0], d1Arr[1], d1Arr[2]);
                    v2 
= new Date(d2Arr[0], d2Arr[1], d2Arr[2]);
                    
if (v2 < v1) {
                        txtDateEnd.addClass(
"fareValidForm");
                        
throw "对不起,结束日期不能小于开始日期!";
                    }
                }

                
var txtRemaindAmt = $("#" + prefix + "txtRemaindAmt");
                
if (txtRemaindAmt.val().match(/^[0-9]+$/== null) {
                    txtRemaindAmt.addClass(
"fareValidForm");
                    
throw "对不起,机票数量必须为数字!";
                }
                
else {
                    
if (txtRemaindAmt.val() < 1) {
                        txtRemaindAmt.addClass(
"fareValidForm");
                        
throw "对不起,机票数量必须大于0!";
                    }
                }

                
var txtFarePrice = $("#" + prefix + "txtFarePrice");
                
if (txtFarePrice.val().match(/^[0-9]+0$/== null) {
                    txtFarePrice.addClass(
"fareValidForm");
                    
throw "对不起,票面价必须为数字,且为10的倍数!";
                }
            });

        } 
catch (e) {
            PopupMsg(e);
            
return false;
        }

        
return CustomConfirm(obj, '您确定要更新吗?');
    }
    
else {
        PopupMsg(
"对不起,您没有修改任何项!");
        
return false;
    }
}

 

 

posted @ 2012-04-19 11:20  青春无敌小宇宙  阅读(206)  评论(0编辑  收藏  举报