Use return true or false in Jquery's each to replace break or continue

Source: Internet
Author: User

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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.