Home  >  Article  >  Web Front-end  >  Use return true or false instead of break or continue_jquery in each of Jquery

Use return true or false instead of break or continue_jquery in each of Jquery

WBOY
WBOYOriginal
2016-05-16 16:47:331208browse
Copy code The code is as follows:

function methodone(){
....
$.each(array,function(){
if(condition is true){
return true;
}
});
....
}

There is an each in a function. If a certain condition in each is true, the function will return true or false

But break and continue cannot be used in the each code block. To implement For the functions of break and continue, you need to use other methods
break----use return false;
continue - use return true;

So when I want to use return true in each When returning to this function, it actually just allows each to continue executing without
even each is interrupted, so the function cannot return.

Solution: Use try to capture throw errors to achieve the goal of exiting each and returning errors!
Copy code The 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 fill in 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 less than the start date!";
}
}

var txtRemaindAmt = $("#" prefix "txtRemaindAmt" );
if (txtRemaindAmt.val().match(/^[0-9] $/) == null) {
txtRemaindAmt.addClass("fareValidForm");
throw "Sorry, ticket Quantity must be a number! ";
}
else {
if (txtRemaindAmt.val() < 1) {
txtRemaindAmt.addClass("fareValidForm");
throw "Sorry, the number of tickets 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 any items!");
return false;
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn