Copy codeThe Code is as follows:
Function methodone (){
....
$. Each (array, function (){
If (condition true ){
Return true;
}
});
....
}
There is an each in a function. If a condition in the each is true, the function returns true or false.
However, you cannot use break and continue in the each code block. To implement the break and continue functions, you must use other methods.
Break ---- return false;
Continue -- use return ture;
Therefore, when I want to use return true in each to return the function to this function, I only want the each to continue to execute
Even the each is not interrupted, so the function cannot return.
Solution: Use try to capture throw errors to exit each and return errors!
Copy codeThe Code is as follows:
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 "sorry, please enter the validity period! ";
}
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 "sorry, the end date cannot be earlier than the start date! ";
}
}
Var txtRemaindAmt = $ ("#" + prefix + "txtRemaindAmt ");
If (txtRemaindAmt. val (). match (/^ [0-9] + $/) = null ){
TxtRemaindAmt. addClass ("fareValidForm ");
Throw "sorry, the ticket quantity must be a number! ";
}
Else {
If (txtRemaindAmt. val () <1 ){
TxtRemaindAmt. addClass ("fareValidForm ");
Throw "sorry, the ticket quantity must be greater than 0! ";
}
}
Var txtFarePrice = $ ("#" + prefix + "txtFarePrice ");
If (txtFarePrice. val (). match (/^ [0-9] + 0 $/) = null ){
TxtFarePrice. addClass ("fareValidForm ");
Throw "sorry, the face price must be a number and a multiple of 10! ";
}
});
} Catch (e ){
PopupMsg (e );
Return false;
}
Return CustomConfirm (obj, 'Are you sure you want to update? ');
}
Else {
PopupMsg ("Sorry, you have not modified anything! ");
Return false;
}
}