jquery验证器——仅对可见元素进行验证

时间:2022-11-28 16:33:32

I have a radiobutton that hides/shows a div. All visible elements are "required", but adding ignore: ":hidden" after the validation rules, doesn't work... Here's the code:

我有一个radiobutton,它隐藏/显示一个div。所有可见的元素都是“必需的”,但是在验证规则之后添加ignore:“:hidden”,行不通…这是代码:

<script type="text/javascript">

        $(document).ready(function(){
            $("#myForm").validate({
                rules: {
                    name: "required",
                    age: "required",
                    height: "required",
                    ignore: ":hidden"
                }
            })
        });

    </script>
</head>
<body>
    <form id="myForm" name="myForm" method="post" action="" >
        <input type="radio" name="checkAge" value="adult" onclick="$('#minorRequisites').hide();" />Adult<br/>
        <input type="radio" name="checkAge" value="minor" onclick="$('#minorRequisites').show()" checked="checked"/>Child<br/>
        Name <input id="name" name="name" type="text" /><br/>
        <div id="minorRequisites">
            Age <input id="age" name="age" type="text" /><br/>
            Height <input id="height" name="height" type="text" /><br/>
        </div>
        <input type="submit" />
    </form>
</body>

I want to be able to submit the form giving only name if "Adult" is checked... Any ideas? :)

如果“成人”被检查,我想能够提交只给出姓名的表格……什么好主意吗?:)

2 个解决方案

#1


11  

You can specify dependency expressions in your required rules to have them ignore the elements if they're not visible:

您可以在所需的规则中指定依赖项表达式,使它们忽略不可见的元素:

$("#myForm").validate({
    rules: {
        name: "required",
        age: {
            required: "#age:visible"
        },
        height: {
            required: "#height:visible"
        }
    }
});

You can see the results in this fiddle.

你可以在这个小提琴里看到结果。

EDIT: ignore will also work, but it's an option, not a rule, so you should write:

编辑:忽略也可以,但这是一个选项,不是规则,所以你应该写:

$("#myForm").validate({
    ignore: ":hidden",
    rules: {
        name: "required",
        age: "required",
        height: "required"
    }
});

#2


0  

Please try ignore: ":not(:visible)" instead of ignore: ":hidden".

请试着忽略:“:not(:visible)”而不是忽略:“:hidden”。

#1


11  

You can specify dependency expressions in your required rules to have them ignore the elements if they're not visible:

您可以在所需的规则中指定依赖项表达式,使它们忽略不可见的元素:

$("#myForm").validate({
    rules: {
        name: "required",
        age: {
            required: "#age:visible"
        },
        height: {
            required: "#height:visible"
        }
    }
});

You can see the results in this fiddle.

你可以在这个小提琴里看到结果。

EDIT: ignore will also work, but it's an option, not a rule, so you should write:

编辑:忽略也可以,但这是一个选项,不是规则,所以你应该写:

$("#myForm").validate({
    ignore: ":hidden",
    rules: {
        name: "required",
        age: "required",
        height: "required"
    }
});

#2


0  

Please try ignore: ":not(:visible)" instead of ignore: ":hidden".

请试着忽略:“:not(:visible)”而不是忽略:“:hidden”。