我遇到了一个小问题,这里我有超过10个表行,但我的代码只执行一次。这是我的代码。请帮帮我
var rowlength = $("#totalLostProductGrid tbody tr ").length;
for(var i=1;i<=rowlength;i++){
var startDateVal = $("#lpostartDate_"+i).val().split(/-|\s|:/);
var endDateVal= $("#lpoendDate_"+i).val().split(/-|\s|:/);
var lostProductValue = $("#lostProductionStartToEnd_"+i).val();
var realEndDateVal = $("#eEndTime").val().split(/-|\s|:/);
var FLNominalvalue = $("#idNominalPower").text();
var idEstimatedLostProdVal = $("#idEstimatedLostProd").val();
var durationTime = ((new Date(endDateVal[2],endDateVal[1],endDateVal[0],endDateVal[3],endDateVal[4],endDateVal[5])) -(new Date(startDateVal[2],startDateVal[1],startDateVal[0],startDateVal[3],startDateVal[4],startDateVal[5])) ) / 1000 / 60 / 60;
var actualVal = durationTime * lostProductValue;
alert(actualVal);
if(actualVal < FLNominalvalue){
alert("success");
return false;
}else{
alert("invalid case");
return false;
}
}
答案 0 :(得分:3)
如果确定
,您无需返回错误$("#totalLostProductGrid tbody tr").each(function(i) {
i++; // assuming your fields start at 1
var startDateVal = $("#lpostartDate_"+i).val().split(/-|\s|:/),
endDateVal= $("#lpoendDate_"+i).val().split(/-|\s|:/),
lostProductValue = $("#lostProductionStartToEnd_"+i).val(),
realEndDateVal = $("#eEndTime").val().split(/-|\s|:/),
FLNominalvalue = $("#idNominalPower").text(),
idEstimatedLostProdVal = $("#idEstimatedLostProd").val(),
durationTime = ((new Date(endDateVal[2],endDateVal[1],endDateVal[0],endDateVal[3],endDateVal[4],endDateVal[5])) -(new Date(startDateVal[2],startDateVal[1],startDateVal[0],startDateVal[3],startDateVal[4],startDateVal[5])) ) / 1000 / 60 / 60;
var actualVal = durationTime * lostProductValue;
console.log(actualVal);
if(actualVal < FLNominalvalue){
console.log(i+":success");
}else{
alert("row #"+i+" contains an invalid case");
return false;
}
});
或仅在错误时返回false:
$("#totalLostProductGrid tbody tr").each(function(i) {
i++; // assuming your fields start at 1
var startDateVal = $("#lpostartDate_"+i).val().split(/-|\s|:/),
endDateVal= $("#lpoendDate_"+i).val().split(/-|\s|:/),
lostProductValue = $("#lostProductionStartToEnd_"+i).val(),
realEndDateVal = $("#eEndTime").val().split(/-|\s|:/),
FLNominalvalue = $("#idNominalPower").text(),
idEstimatedLostProdVal = $("#idEstimatedLostProd").val(),
durationTime = ((new Date(endDateVal[2],endDateVal[1],endDateVal[0],endDateVal[3],endDateVal[4],endDateVal[5])) -(new Date(startDateVal[2],startDateVal[1],startDateVal[0],startDateVal[3],startDateVal[4],startDateVal[5])) ) / 1000 / 60 / 60;
var actualVal = durationTime * lostProductValue;
if(actualVal >= FLNominalvalue){
alert("row #"+i+" contains an invalid case");
return false;
}
});
答案 1 :(得分:2)
在循环结束时,你将从中退出,基本上退出它。
另外,你确定你打算从索引1开始而不是0吗? (var i = 0)
答案 2 :(得分:0)
if(actualVal < FLNominalvalue){
alert("success");
return false;
}else{
alert("invalid case");
return false;
}
这意味着无论结果是什么,它都将返回false,这将结束foreach