[英]jQuery Validate not showing all error messages at once, only displaying one at a time
I've searched the web and read through the jQuery Validate Docs and I'm not finding out what I'm doing wrong.我在网上搜索并通读了jQuery Validate Docs ,但我没有发现我做错了什么。
I have a form in a modal that is using jQuery Validate and everything works now.我有一个使用jQuery Validate的模态表单,现在一切正常。 When I try to submit the form without the required fields, the text boxes turn red.当我尝试提交没有必填字段的表单时,文本框变为红色。 However only one error message pops up, it looks like this:但是只弹出一个错误消息,它看起来像这样:
When I click on the other textbox
's (ie: from the security question textbox
to the password textbox
) the error message for the text box pops up and I click into, but the other textbox
's error message goes away.当我单击另一个textbox
(即:从安全问题textbox
到密码textbox
)时,会弹出该文本框的错误消息,然后单击进入,但另一个textbox
的错误消息消失了。
It should show " Answer Required " and " Password Required " in the error message div
.它应该在错误消息div
显示“需要回答”和“需要密码”。 I have tried using the Validate plugin's errorPlacement
, groups
, and other things without success.我曾尝试使用Validate插件的errorPlacement
、 groups
和其他东西,但没有成功。
My script looks like this:我的脚本如下所示:
$('#QuestionAnswer').validate({
rules: {
question: {
required: true,
notEqual: '#answer'
},
answer: {
required: true
},
password: {
required: true
}
},
messages: {
question: {
required: "Question Required",
notEqual: 'Question cannot be the same as Answer'
},
answer: {
required: "Answer Required"
},
password: {
required: "Please enter the user's password"
errorPlacement: function (error, element) {
$("#qaErrorLabel").html(error);
$("#qaErrorDiv").show();
}
});
My HTML looks like this:我的 HTML 如下所示:
<div id="qaModaldiv" style="display:none;">
@using (Html.BeginForm("QuestionAnswer", "Controller", FormMethod.Post, new
{
id = "QuestionAnswer",
Name = "QuestionAnswer"
}))
{
<div id="errorMsgCenter" style="font-size:12px;">
@Html.ValidationSummary()
</div>
<div id="qaErrorDiv" class="error" style="font-size: 12px; display:none;">
<label id="qaErrorLabel" class="validation-summary-errors"></label>
</div>
<div class="panel" style="margin:25px;">
<table>
<tr>
<td>Security Question:</td>
<td> <input type="text" style="width:250px;" name="question" id="question" value="@Model.Question" maxlength="64" /></td>
</tr>
<tr>
<td>Security Answer: </td>
<td> <input type="text" style="width:250px;" name="answer" id="answer" maxlength="64" /></td>
</tr>
<tr>
<td>User's Password: </td>
<td> <input type="password" name="password" id="password" /></td>
</tr>
</table>
</div>
}
When I inspect element on the error that does pop up, none of the other errors are there, only the one is there.当我检查弹出错误的元素时,没有其他错误存在,只有一个错误存在。 I have a sneaking suspicion I'm doing something wrong with the errorPlacement
part, but I've played with that and can't get anything to work.我偷偷怀疑我在errorPlacement
部分做错了什么,但我已经玩过了并且无法得到任何工作。
Any help is appreciated.任何帮助表示赞赏。
Thanks!谢谢!
That's because you are overwriting the previous message with a new message.那是因为您正在用新消息覆盖之前的消息。 One element only has one .innerHTML
property.一个元素只有一个.innerHTML
属性。 errorPlacement
is called once for each error. errorPlacement
为每个错误调用一次。 Each time the method is called, the innerHTML
of the qaErrorLabel
element is replaced with a new content.每次调用该方法时, qaErrorLabel
元素的innerHTML
qaErrorLabel
被替换为新的内容。 That's why you see only one message printed on the screen.这就是为什么您只能在屏幕上看到一条消息。
Here is exactly what I did to fix the error.这正是我为修复错误所做的。 I changed .html
to .append
.我将.html
更改为.append
。 However by doing this the errors are not on a new line, they are right next to each other like this:但是,通过这样做,错误不会在新行上,它们像这样彼此相邻:
My first error message.Mysecond error message.我的第一条错误信息。我的第二条错误信息。
Rather than this:而不是这样:
My first error message.我的第一条错误信息。
My second error message.我的第二条错误信息。
So to fix that I changed the code to look like this:所以为了解决这个问题,我将代码更改为如下所示:
messages: {
question: {
required: "Question Required<br/>",
notEqual: 'Question cannot be the same as Answer<br/>'
},
answer: {
required: "Answer Required<br/>"
},
password: {
required: "Please enter the user's password<br/>"
errorPlacement: function (error, element) {
$("#qaErrorLabel").append(error);
$("#qaErrorDiv").show();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.