我正在使用以下 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">4</td>
<td class="noofmarkstd q1_ans_text" q_group="1" rowspan="2"><strong>4</strong></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>0</strong></td>
</tr>
</tbody>
</table>
下面是表格的样子:
Marks Per Answer Total Marks Marks Remaining
(blank text input) 4 4
(value=6 text input) 6 0
我试图验证表格,以便如果文本input
为空白,则显示一条消息。如果 'Marks Remaining' 不等于 0,则显示一条验证消息(如果大于 0,则显示一条验证消息,如果小于 0,则显示一条不同的验证消息)。
问题是它没有输出任何验证消息。为什么它不执行任何验证?
这是我的 jQuery 代码:
function validation() {
var alertValidation = "";
var _qid = "";
var _msg = "";
var questions = $('#markstbl td[class*="_ans"]').length;
$("[class*='q'").each(function (i) {
var marks = parseInt($("[class*=q" + i + "_mark]").text());
_qid = $(".q" + i + "_ans_org").text();
_msg = "You have errors on Question Number: " + _qid + "\n";
if (!this.value) {
alertValidation += "\n\u2022 You have not entered in a value in the Indivdiaul Marks textbox all your Answers\n";
}
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;
}