只是对我的验证有一点问题。我想要发生的是,我想通过在底部显示验证消息来检查是否首先添加了所有行,说明我还有很多问题。只有当所有问题都已添加并且验证已通过时,它才会显示每个单独问题的验证,例如请选择至少一个答案或您选择的答案少于所需数量。
目前它正在以相反的方式进行操作,例如它声明我需要选择至少一个答案,然后当它被排序时,它声明我需要添加其余的问题。
function validation() {
var marks = parseInt($("#total-weight").text());
var _qid = "";
var _msg = "";
var maxQuestions = <? php echo(int) $_SESSION['textQuestion']; ?> ;
var questionsAdded = $('tr.optionAndAnswer').length;
var alertValidation = "";
// Note, this is just so it's declared...
$("tr.optionAndAnswer").each(function () {
_qid = $("td.qid", this).text();
_msg = "You have errors on Question Number: " + _qid + "\n";
$(".numberAnswerTxtRow", this).each(function () {
var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length;
if ($(this).val() == 0) {
alertValidation += "\n\u2022 You have not selected an answer, please select at least one answer\n";
} else if (currenttotal < $(this).val()) {
alertValidation += "\n\u2022 You have selected less answers than the required amount\n";
}
if (alertValidation != "") {
return false; //Stop the each loop
}
});
});
if (alertValidation == "") {
if (questionsAdded < maxQuestions) {
_msg = '';
alertValidation = ("You Have Not Added in All of Your Questions \n\n\u2022 You have " + (maxQuestions - questionsAdded) + " Questions Remaining:");
}
}
if (alertValidation != "") {
alert(_msg + alertValidation);
return false;
}
return true;
}
下面是整个代码- 在下面的代码中,它不会显示其他验证消息,直到首先通过剩余验证的问题数。像我提到的唯一问题是其中的验证$(".numberAnswerTxtRow",this).each(function() {
也是其他包含 else if 的函数中唯一的函数:
function validation() {
var marks = parseInt($("#total-weight").text());
var _qid = "";
var _msg = "";
var maxQuestions = <? php echo(int) $_SESSION['textQuestion']; ?> ;
var questionsAdded = $('tr.optionAndAnswer').length;
var alertValidation = "";
// Note, this is just so it's declared...
$("tr.optionAndAnswer").each(function () {
_qid = $("td.qid", this).text();
_msg = "You have errors on Question Number: " + _qid + "\n";
$(".textAreaQuestion", this).each(function () {
if (!this.value || this.value.length < 5) {
alertValidation += "\n\u2022 You have not entered a valid Question\n";
}
if (alertValidation != "") {
return false; //Stop the each loop
}
});
$(".numberAnswerTxtRow", this).each(function () {
var currenttotal = $(this).closest('.optionAndAnswer').find('.answerBtnsOn').length;
if ($(this).val() == 0) {
alertValidation += "\n\u2022 You have not selected an answer, please select at least one answer\n";
} else if (currenttotal < $(this).val()) {
alertValidation += "\n\u2022 You have selected less answers than the required amount\n";
}
if (alertValidation != "") {
return false; //Stop the each loop
}
});
$(".txtWeightRow", this).each(function () {
if (!this.value) {
alertValidation += "\n\u2022 Please enter in a figure for Number of Marks for this Question\n";
}
if (alertValidation != "") {
return false; //Stop the each loop
}
});
if (alertValidation != "") {
return false;
}
});
if (alertValidation == "") {
if (questionsAdded < maxQuestions) {
_msg = '';
alertValidation = ("You Have Not Added in All of Your Questions \n\n\u2022 You have " + (maxQuestions - questionsAdded) + " Questions Remaining:");
}
}
if (alertValidation != "") {
alert(_msg + alertValidation);
return false;
}
return true;
}