我正在使用以下 HTML 代码来显示表格:
<form id="Marks" action="/u0877654/Mobile/individualmarks.php" method="post">
<table border='1' id='markstbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='noofmarksth'>Marks Remaining</th>
</tr>
</thead>
<tbody>
<tr class="questiontd">
<td class="questionnumtd q<?php echo$questionId?>_qnum" name="numQuestion" rowspan="1">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="5"><input type="hidden" name="q1_ans" class="q1_ans" value="5"></td>
<td class="answermarkstd">
<input class="individualMarks q1_mark" q_group="1" name="answerMarks[]" class="individualtext" type="text" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
<td class="noofmarkstd q1_ans_text" q_group="1" rowspan="1"><strong>5</strong></td>
</tr>
<tr class="questiontd">
<td class="questionnumtd q<?php echo$questionId?>_qnum" name="numQuestion" rowspan="1">2 <input type="hidden" name="q2_ans_org" class="q2_ans_org" value="5"><input type="hidden" name="q2_ans" class="q2_ans" value="5"></td>
<td class="answermarkstd">
<input class="individualMarks q2_mark" q_group="1" name="answerMarks[]" class="individualtext" type="text" onkeypress="return isNumberKey(event)" maxlength="3" />
</td>
<td class="noofmarkstd q2_ans_text" q_group="1" rowspan="1"><strong>5</strong></td>
</tr>
</tbody>
</table>
<p><input id="submitBtn" name="submitMarks" type="submit" value="Submit Marks" /></p>
</form>
下面是表格的样子:
Question No. Marks Per Answer Marks Remaining
1 (value=5 text input) 0
2 (blank text input) 5
现在我已经使用 jquery 为上面的表创建了验证。如果一行包含除 0 以外的任何数字(更高或更低),那么它将显示一条警报,说明:
大于 0:
You have errors on Question Number:
Your Total Marks Remaining does not equal 0
• You Have NaN Marks Remaining
小于 0:
You have errors on Question Number:
Your Total Marks Remaining does not equal 0
• You Need To Remove NaN Marks
另一个验证是,如果用户在“Marks Per Answer”列下将文本输入留空,则它应该在下面显示此消息:
You have errors on Question Number:
• You have not entered in a value for all the Indivdiaul Marks textbox
如您所见,我有两个问题。首先它不显示问题编号包含错误的编号,它应该显示包含错误的第一个问题的问题编号。
另一个问题是它一直显示 NaN。它应该显示需要删除多少标记才能使其变为 0 或添加多少标记才能使其变为 0。
上面的两个问题怎么解决?
这是我的 jQuery 代码:
function validation() {
var alertValidation = "";
var _qid = "";
var _msg = "";
$("[class*='q']").each(function (i) {
var questions = parseInt($("[class*=q" + i + "_qnum]").text());
var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
var txtinput = $("[class*=q" + i + "_mark]").val();
_qid = questions;
_msg = "You have errors on Question Number: " + _qid + "\n";
if (txtinput == '' || txtinput == null) {
alertValidation += "\n\u2022 You have not entered in a value for all the Indivdiaul Marks textbox\n";
} else if (marks < '0') {
alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
} else if (marks > '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;
}