我正在使用以下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;
}
答案 0 :(得分:1)
你的jQuery代码确实有错误。此选择器$("[class*='q'")
无效。它必须是这样的:$("[class*='q']")
。
以下是运行代码的jsfiddle:http://jsfiddle.net/jYZSX/
我仍在阅读问题,以确定验证应如何运作。