這是我的jQuery代碼來驗證HTML表:如何正確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並且用於當標記的數目remainig低於0作品完美時輸出警報,因爲它顯示此下面的驗證:
高於0:
You have errors on Question Number: 1
Your Total Marks Remaining does not equal 0
• You Have 7 Marks Remaining
小於0:
You have errors on Question Number: 1
Your Total Marks Remaining does not equal 0
• You Need To Remove 3 Marks
但是,當我嘗試驗證,看看是否「標誌每答案」部分下的文本輸入是空的,那麼它顯示下面這條消息:
You have errors on Question Number: NaN
• You have not entered in a value for all the Indivdiaul Marks textbox
爲什麼不能顯示此驗證的問題編號?
另外我遇到的一個小問題是,即使我在「Marks Per Answer」下的所有文本輸入中輸入了一個值,它仍然在不應該顯示上述驗證消息警報時顯示。爲什麼它也這樣做?
下面是一個例子HTML表格:
<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>
</tbody>
</table>
嗨,但我沒有得到的是,它如何是NaN的這種驗證,但對於其他驗證它顯示問題編號?我已經在一個更大的表上測試了它的記錄,它對於高於或者低於0的驗證是完美的,但是當驗證空文本輸入時,它顯示NaN作爲問題編號。我的方式是否正確並檢查「每標記標記」列下的空白文本輸入是否正確? – user1861818