Jquery's each USES return true or false instead of break or continue

  • 2020-03-30 03:01:38
  • OfStack

 
function methodone(){ 
.... 
$.each(array,function(){ 
if( Conditions established ){ 
return true; 
} 
}); 
.... 
} 

There is a "each" in a function. If a condition is true in "each", the function returns true or false

However, you cannot use break and continue within each block, and you can use other methods to implement the break and continue functionality
Break - in return false;
Continue -- return true;

So when I want to return true to this function in each, I'm just letting each continue
Each is not interrupted, so function cannot return.

Solution: try to catch throw's errors to exit each and return the wrong goal!
 
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 expiry date! "; 

} 
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, the number of tickets 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 value has to be a number and is 10 A multiple of! "; 
} 
}); 

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

return CustomConfirm(obj, ' Are you sure you want to update it? '); 
} 
else { 
PopupMsg(" Sorry, you didn't change anything! "); 
return false; 
} 
} 

Related articles: