简体   繁体   English

jQuery Validate 不一次显示所有错误消息,一次只显示一个

[英]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插件的errorPlacementgroups和其他东西,但没有成功。

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.

相关问题 jquery.validate不显示消息 - jquery.validate not showing messages 始终仅显示10条最近的服务器已发送事件消息 - Displaying of only 10 recent Server Sent Event Messages all the time 使用jQuery验证并非所有错误消息都会出现并且输入字段会消失 - Using jQuery validate not all error messages appear and input fields disappear jQuery验证不显示错误 - JQuery validate not showing error 如何仅将验证消息与所有错误消息一起显示一次 - How to display validation message only once with all error messages 一次全部显示表单中缺少字段的错误消息 - Displaying error messages for missing fields in a form all at a time 如何在表单顶部显示所有适用的错误消息,而不是一一显示错误消息? - How to show all the applicable error messages on top of a form instead of showing the error messages one by one? 在悬停时显示 div,仅当前显示(一次一个,而不是一次全部显示) - Show div on hover, of current only (one at a time, not all at once) jquery validate plugin,用css一次显示一个错误 - jquery validate plugin, display one error at a time with css 如何在每次输入字段发生错误时使用jQuery Validate显示和隐藏自定义错误消息? - How to show and hide custom error messages with jQuery Validate every time an error occurs on an input field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM