1.jQuery中each语句中使用return

function submitBeforeCheck(rows){
    if(rows.length==0){
        return {"msg":"请选中数据后提交审批","fail":true};
    }
    $(rows).each(function(index,row){
        if(row["isClarify"]=="0")
            return {"msg":"提交须填写是否同意澄清","fail":true};
        if(row["isClarify"]=="1"&&$.trim(row["causeType"])=="")
            return {"msg":"同意澄清须填写原因分类","fail":true};
    });
    return {"fail":false};
}

本意是使用each校验结果集返回校验结果,发现和自己期望不一样,查过资料后发现在each语句块中

return false = break
return ture = continue

在each里使用 return 给整个函数返回时,其实只是跳出each循环而已

以下是解决办法

function submitBeforeCheck(rows){
	if(rows.length==0){
		return {"msg":"请选中数据后提交审批","fail":true};
	}
	try{
		$(rows).each(function(index,row){
			if(row["isClarify"]=="0")
				throw {"msg":"提交须填写是否同意澄清","fail":true};
			if(row["isClarify"]=="1"&&$.trim(row["causeType"])=="")
				throw {"msg":"同意澄清须填写原因分类","fail":true};
		});
	}catch(token){
		return token;
	}
	return {"fail":false};
}

  

原文地址:https://www.cnblogs.com/black-/p/8965894.html