我想在警报中连接消息,以便它显示遇到错误的第一个问题的所有错误消息。例如,如果我有3个问题且问题1和问题3中存在错误,则当我单击提交按钮时,验证警报应显示示例警报,如下所示:
您在问题编号上有错误:1
•您尚未在所有Indivdiaul Marks文本框中输入值
•您尚未在所有Indivdiaul Marks文本框中输入值 您的剩余总分不等于0
只有在我解决了问题1中的错误后,当我再次提交页面时,它才显示问题编号3的警告,如下所示:
您在问题编号上有错误:3
•您尚未在所有Indivdiaul Marks文本框中输入值
•您尚未在所有Indivdiaul Marks文本框中输入值 您的剩余总分不等于0
我遇到的问题是,它一次只能显示1个问题编号,但它显示了问题1和3中的所有错误,所以它如下所示:
您在问题编号上有错误:1
•您尚未在所有Indivdiaul Marks文本框中输入值
•您尚未在所有Indivdiaul Marks文本框中输入值 您的剩余总分不等于0
•您尚未在所有Indivdiaul Marks文本框中输入值
•您尚未在所有Indivdiaul Marks文本框中输入值 您的剩余总分不等于0
我的问题是,我如何才能使警报正常工作,以便符合我希望它的工作方式?
以下是jquery代码:
function validation() {
var alertValidation = "";
var _qid = "";
var _msg = "";
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
var txtinput = $(this).val();
_qid = questions;
_msg = "You have errors on Question Number: " + _qid + "\n";
if (txtinput == '') {
alertValidation += "\n\u2022 You have not entered in a value in 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";
}
if(marks > '0') {
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
});
//comment
if (alertValidation != "") {
alert(_msg + alertValidation);
return false;
}
return true;
}
在上面的代码中显示//comment
,如果我包含此代码:
if (alertValidation != "") {
return false; //Stop the each loop
}
然后它的作用是每个问题每个警报只显示一条消息,例如
除警报外:
您在问题编号上有错误:1
•您尚未在所有Indivdiaul Marks文本框中输入值
•您尚未在所有Indivdiaul Marks文本框中输入值 您的剩余总分不等于0
它会警告:
您在问题编号上有错误:1
•您尚未在所有Indivdiaul Marks文本框中输入值
UPDATE:
以下是代码的更新:
功能验证(){
var alertValidation = "";
var _qid = "";
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + i + "_ans_text]").text());
var txtinput = $(this).val();
_qid = questions;
alertValidation += "\nYou have errors on Question Number: " + _qid + "\n";
if (txtinput == '') {
alertValidation += "\n\u2022 You have not entered in a value in 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";
}
if(marks > '0')
{
alertValidation += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks + " Marks Remaining";
}
});
if (alertValidation != "") {
alert(alertValidation.substr(1));
return false;
}
return true;
}
但问题是下面是警告:
•问题编号有错误:1
•您尚未在所有Indivdiaul Marks文本框中输入值 您在问题编号上有错误:1
•您尚未在所有Indivdiaul Marks文本框中输入值 您的剩余总分不等于0
•您有5个留下的标记您在问题编号上有错误:2
•您在问题编号上有错误:3
•您尚未在所有Indivdiaul Marks文本框中输入值 您的剩余总分不等于0
•您有5个留下的标记您在问题编号上有错误:3
•您尚未在所有Indivdiaul Marks文本框中输入值 您在问题编号上有错误:3
•您尚未在所有Indivdiaul Marks文本框中输入值
答案 0 :(得分:2)
您在每_msg
次迭代中重置.each()
。如果您只是逐个问题地将邮件的_msg
部分添加到alertValidation
,那么应该没问题。
不确定此代码是否有效,因为我没有测试页面。
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(), 10);
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 += "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) + " Marks";
} else if (marks > 0) {
msg += "Your Total Marks Remaining does not equal 0 \n\n\u2022 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;
}
编辑:修正了parseInt(...)
声明的错误。
更新了jsFiddle
答案 1 :(得分:0)
你的问题就在这一行:
_msg = "You have errors on Question Number: " + _qid + "\n";
这将覆盖任何现有文本。因此,您需要做的是将所有文本写入单个变量(而不是_msg
和 alertValidation
)并始终附加:
alertValidation += "\nYou have errors on Question Number: " + _qid + "\n";
然后
alert(alertValidation.substr(1));