我正在使用以下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
另一个验证是,如果用户在“每个答案标记”列下留下了文本输入空白,那么它应该在下面显示以下消息:
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;
}
答案 0 :(得分:1)
当您执行$("[class*=q" + i + "_mark]").text() < '0'
时,您正在比较字符串,因此请尝试使用!=(不等于)运算符与&lt; (少于)。例如:
$("[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 for all the Indivdiaul Marks textbox\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 {
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
}
});
否则使用你的变量:
$("[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 for all the Indivdiaul Marks textbox\n";
}
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
}
});
答案 1 :(得分:1)
您需要使用.val()从输入中检索值:
_qid = $(".q" + i + "_ans_org").val();
_msg = "You have errors on Question Number: " + _qid + "\n";
编辑:您使用的选择器是什么?类选择器前面只有一个点:
var marks = parseInt($(".q" + i + "_ans_text").text());