0
下面我通過每一個問題for循環使用的循環:我得到了我未定義的錯誤在環
現在我想要做的就是創建一個驗證功能,使得它驗證通過每個問題,每道題中的每個元素,但問題是,我想通過每行使用foreach功能循環,像這樣:
function validation() {
var alertValidation = "";
var _qid = "";
var _msg = "";
var marks = parseInt($("[class*=q" + i + "_mark]").text());
_qid = $(".q"+questionno+"_ans_org").text();
_msg = "You have errors on Question Number: " + _qid + "\n";
$("[class*=q" + i + "_mark]").each(function() {
if (!this.value) {
alertValidation += "\n\u2022 You have not entered in a value in the Indivdiaul Marks textbox all your Answers\n";
}
if (alertValidation != "") {
return false; //Stop the each loop
}
});
$(".q"+questionno+"_ans_org").each(function(){
if($("[class*=q" + i + "_mark]").text() < '0')
{
alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
}
else if($("[class*=q" + i + "_mark]").text() > '0')
{
alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
if (alertValidation != "") {
return false; //Stop the each loop
}
});
if (alertValidation != "") {
alert(_msg + alertValidation);
return false;
}
return true;
}
很顯然,因爲我使用foreach方法,該i
是不確定的。現在我的問題是,首先,如果我使用foreach循環或上面的驗證函數的for循環更好,並且沒有人知道如何修復上面的代碼,以便代碼工作以便我不會得到一個i
未定義的錯誤?
下面是html代碼:
<table border='1' id='markstbl'>
<thead>
<tr>
<th class='answermarksth'>Marks per Answer</th>
<th class='totalmarksth'>Total Marks</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>
<tr class="questiontd">
<td class="answermarkstd">
<input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="2">6</td>
<td class="noofmarkstd q1_ans_text" q_group="1" rowspan="2"><strong>5</strong></td>
</tr>
<tr class="questiontd">
<td class="answertd" name="answers[]">D</td>
<td class="answermarkstd">
<input class="individualMarks q1_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
</tr>
<tr class="questiontd">
<td class="answermarkstd">
<input class="individualMarks q2_mark_0" q_group="1" name="answerMarks[]" id="individualtext" type="text" />
</td>
<td class="totalmarkstd" rowspan="1">6</td>
<td class="noofmarkstd q2_ans_text" q_group="1" rowspan="1"><strong>5</strong></td>
</tr>
</tbody>
</table>