简体   繁体   English

jQuery验证的奇怪行为

[英]Strange Behavior of jQuery Validate

I use jQuery Validate and when I submit the form it shows just one required field and it shows others only when I click on them, and on the next one. 我使用jQuery Validate,当我提交表单时,它仅显示一个必填字段,并且仅当我单击它们时才显示其他字段。

This is the script and the body: 这是脚本和正文:

 jQuery(function () {
        $("#form1").validate({
            rules: {
                Yritys: {
                    required: true,
                    minlength: 3
                },
                Henkilönimi: {
                    required: true,
                    minlength: 3
                },
                Asema_yrityksessa: {
                    required: true,
                    minlength: 3
                },
                Puhelin_Nr: {
                    required: true,
                    minlength: 3
                },
                e_Mail: {
                    required: true,
                    minlength: 3
                },
                Keskustelun_aihe: {
                    required: true,
                    minlength: 10
                },
                messages: {
                    Yritys: {
                        required: "You must enter something",
                        minlength: "You must enter more than 3 characters"
                    },
                    Henkilönimi: {
                        required: "You must enter something",
                        minlength: "You must enter more than 3 characters"
                    },
                    Asema_yrityksessa: {
                        required: "You must enter something",
                        minlength: "You must enter more than 3 characters"
                    },
                    Puhelin_Nr: {
                        required: "You must enter something",
                        minlength: "You must enter more than 3 characters"
                    },
                    e_Mail: {
                        required: "You must enter something",
                        minlength: "You must enter more than 3 characters"
                    },
                    Keskustelun_aihe: {
                        required: "You must enter something",
                        minlength: "You must enter more than 10 characters"
                    }
                }
            },

        });

    });


 <div class="divInfo">
        <form id="form1">
            <span>Yritys:</span><br />
            <input id="Yritys" type="text" required /><br />
            <span>Henkilönimi:</span><br />
            <input id="Henkilönimi" type="text" required /><br />
            <span>Asema yrityksessa:</span><br />
            <input id="Asema_yrityksessa" type="text" required /><br />
            <span>Puhelin Nr:</span><br />
            <input id="Puhelin_Nr" type="text" required /><br />
            <span>e-Mail:</span><br />
            <input id="e_Mail" type="text" required /><br />
            <span>Keskustelun aihe:</span><br />
            <textarea id="Keskustelun_aihe" maxlength="140" style="width:300px;height:150px;" required>Enter Text Here...</textarea><br />
            <span style="color:red; margin-left:0;">Keskustelun aihe max 140 merkkia</span>
            <div id="textarea_feedback" style="opacity:0.5"></div>
            <div class="butt"><pre style="font-size:x-large">Vahvista varaus klikkamalla tässä>>>>>>>>>>>>><button id="submit" type="submit" name="appointment" value="" class="btn-warning" /></pre></div>

        </form>
    </div>

Why does this happen? 为什么会这样? It works, but it's not working properly. 它可以工作,但是不能正常工作。 I can't find the solution. 我找不到解决方案。

EDIT: 编辑:

THIS HAPPENS: 有时候是这样的:

在此处输入图片说明

Why does this happen? 为什么会这样? It works, but it's not working properly ... 它可以工作,但是不能正常工作...

You MUST have a name attribute on every input considered for validation, and the name is the only thing that can be referenced from the rules object of .validate() . 必须在考虑进行验证的每个输入上都具有name属性,并且该name是唯一可以从.validate()rules对象引用的name

$("#form1").validate({
    rules: {
        Yritys: {      // <- this must be the NAME
            required: true,
            minlength: 3
        },
        Henkilönimi: { // <- this must be the NAME
            ....

... I can't find the solution ...我找不到解决方案

Official Documentation: Markup recommendations - 官方文档:标记建议 -

Mandated : A 'name' attribute is required for all input elements needing validation, and the plugin will not work without this. 强制 :所有需要验证的输入元素都必须具有“名称”属性,如果没有此属性,插件将无法工作


You also accidentally put the messages object inside of rules , which are supposed to be siblings. 您还无意中将messages对象放置在rulesrules应该是同级的。

$("#form1").validate({
    rules: {
        ....
    },
    messages: {
        ....
    }
});

DEMO : jsfiddle.net/2o9Lxkej 演示jsfiddle.net/2o9Lxkej

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM