Jquery的each里用return true或false代替break或continue
更新時間:2014年05月21日 09:25:13 作者:
Jquery的each里面用return false代替break;return ture 代替continue
復制代碼 代碼如下:
function methodone(){
....
$.each(array,function(){
if(條件成立){
return true;
}
});
....
}
在一個function里有一個each,在each里某種條件 成立的話,就把這個function返回true或者false
但是在each代碼塊內(nèi)不能使用break和continue,要實現(xiàn)break和continue的功能的話,要使用其它的方式
break----用return false;
continue --用return ture;
所以當我在each里想使用return true給這個function返回時,其實只是讓each繼續(xù)執(zhí)行而以
連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 "對不起,結(jié)束日期不能小于開始日期!";
}
}
var txtRemaindAmt = $("#" + prefix + "txtRemaindAmt");
if (txtRemaindAmt.val().match(/^[0-9]+$/) == null) {
txtRemaindAmt.addClass("fareValidForm");
throw "對不起,機票數(shù)量必須為數(shù)字!";
}
else {
if (txtRemaindAmt.val() < 1) {
txtRemaindAmt.addClass("fareValidForm");
throw "對不起,機票數(shù)量必須大于0!";
}
}
var txtFarePrice = $("#" + prefix + "txtFarePrice");
if (txtFarePrice.val().match(/^[0-9]+0$/) == null) {
txtFarePrice.addClass("fareValidForm");
throw "對不起,票面價必須為數(shù)字,且為10的倍數(shù)!";
}
});
} catch (e) {
PopupMsg(e);
return false;
}
return CustomConfirm(obj, '您確定要更新嗎?');
}
else {
PopupMsg("對不起,您沒有修改任何項!");
return false;
}
}
相關文章
JQuery select控件的相關操作實現(xiàn)代碼
JQuery獲取和設置Select選項方法匯總?cè)缦?,需要的朋友可以參考?/div> 2012-09-09webuploader模態(tài)框ueditor顯示問題解決方法
這篇文章主要為大家詳細介紹了webuploader模態(tài)框ueditor顯示問題的解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12easyui-datagrid開發(fā)實踐(總結(jié))
本篇文章主要介紹了easyui-datagrid開發(fā)實踐(總結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08使用jquery與圖片美化checkbox和radio控件的代碼(打包下載)
用jquery實現(xiàn)的對checkbox和radio控件的美化,非常不錯,大家可以通過修改圖片來實現(xiàn)更漂亮的效果。2010-11-11基于jquery的兼容各種瀏覽器的iframe自適應高度的腳本
在網(wǎng)上找了很多的iframe自適應高度的腳本,對瀏覽的的兼容性都不好。所以就想利用jquery強大的兼容性,寫一個iframe自適應高度的腳本。2010-08-08分享有關jQuery中animate、slide、fade等動畫的連續(xù)觸發(fā)、滯后反復執(zhí)行的bug
這篇文章主要介紹了分享有關jQuery中animate、slide、fade等動畫的連續(xù)觸發(fā)、滯后反復執(zhí)行的bug的相關資料,需要的朋友可以參考下2016-01-01最新評論