请参阅下面的js验证功能。
function validate_submit(PassForm) {
var bGo = false;
var rankcount = document.getElementById('rankCount').value;
var j = 0;
var iRankcount0 = document.getElementById('indRankcount0').value;
var iRankcount1 = document.getElementById('indRankcount1').value;
var iRankcount2 = document.getElementById('indRankcount2').value;
var ijs = 0;
var itemp = ijs;
for (i = 0; i < rankcount; i++) {
alert("begin i = " + i);
if (i == 0) {
indRankcount = iRankcount0;
}
else if (i == 1) {
alert('indRankcount: ' + indRankcount);
indRankcount = iRankcount1;
alert('iRankcount1: ' + iRankcount1);
alert('indRankcount: ' + indRankcount);
}
else if (i == 2) {
indRankcount = iRankcount2;
}
alert('before sec loop indRank: ' + indRankcount);
alert('before sec loop itemp: ' + itemp);
for (k = itemp; k < indRankcount; k++) {
alert('in check bGo');
if (document.getElementById("selectedScore" + i + k).checked) {
bGo = true;
j++;
} //if
} //for indRankcount - k loop
if (bGo) {
if (i == 0) {
par = (Math.ceil(indRankcount / 4));
}
else if (i == 1) {
par = (Math.ceil((iRankcount1 - iRankcount0) / 4));
alert('1: ' + par);
}
else if (i == 2) {
par = (Math.ceil((indRankcount2 - iRankcount1) / 4));
}
if (j == par) {
j = 0;
bGo = false;
itemp = indRankcount;
alert("itemp = " + itemp);
continue;
}
else {
alert('25% criteria not met.');
return false;
}
}
else { //else to check bGo
alert('Atleast one box need to be selected.');
return false;
}
j = 0;
bGo = false;
itemp = indRankcount;
alert("loop ends: i =" + i);
} //for rankcount - i loop
res = window.confirm('Are you sure you want to proceed with the selection?');
if (res) {
return true;
}
else {
return false;
}
} //end of validate
问题是当i = 0时,它执行正常。但是当i = 1时,第二个循环(K)不执行(我们将变量切换为常量 - 它适用于itemp或indRankcount。只有一个数字可以执行它。)它完全跳过。请帮忙!谢谢!
答案 0 :(得分:1)
在内循环(使用“k”)之后,有一个“itemp = indRankcount;”线。我想这会导致问题。
在第一次运行时,“itemp”为0,因此内部循环进入,但在第二次运行时,此值大于或等于“indRankcount”,因为您之前调用了代码。 “iRankcount0”,“iRankcount1”和“iRankcount2”中存储了哪些值? 尝试在2.循环之前打印“itemp”和“indRankcount”值。
更新后,在k循环之前尝试此操作,它将显示为什么k不会在2.执行时启动。
Console.log(i + "loop:: " + itemp + " val (k first val), " + " indRankcount " + val (k end val));