当问题中的任何文本输入为空时,我想在警报中生成验证消息。因此,例如,如果问题 1 有 2 个空白文本输入,如果两个文本输入均为空白,则会显示验证消息You have not entered in a value in all the Indivdiaul Marks textbox
。
但问题是,例如,对于问题 1,如果 1 个文本输入为空白但另一个不是空白,则它不会显示验证消息,即使它应该这样做,因为并非所有文本输入都已为问题 1 填写。
我的问题是,如果每个问题有任何空白文本输入,我如何才能显示验证消息?
这是一个小提琴,因此您可以对其进行测试:http: //jsfiddle.net/cbyJD/87/
下面是validation()函数代码:
function validation() {
// only keeping track of the final message
var alertValidation = "",
// toggle for showing only one error
showOnlyOneError = true;
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + (i+1) + "_ans_text]").text());
var txtinput = $(this).val();
// the message for this question
var msg = '';
if (txtinput == '') {
msg += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox \n";
}
if (marks < 0) {
msg += "\n\u2022 Your Total Marks Remaining does not equal 0 \n - You Need To Remove " + Math.abs(marks) + " Marks";
} else if (marks > 0) {
msg += "\n\u2022 Your Total Marks Remaining does not equal 0 \n - You Have " + marks + " Marks Remaining";
}
// if there is an error for the question, add it to the main message
if (msg.length) {
alertValidation += alertValidation.length ? '\n\n' : '';
alertValidation += "You have errors on Question Number: " + questions + "\n";
alertValidation += msg;
// stop if we only care about the first error
return !showOnlyOneError;
}
});
// show the error messages
if (alertValidation != "") {
alert(alertValidation);
return false;
}
return true;
}