javascript 选择框上的 jQuery 验证不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13945698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:16:03  来源:igfitidea点击:

jQuery validation on select box not working

javascriptjquery

提问by Surinder ツ

I am using jQuery validation plugin for client side validation, but my validation does not work on my select box.

我正在使用 jQuery 验证插件进行客户端验证,但我的验证在我的选择框上不起作用。

HTML

HTML

<select id="select" class="required">
    <option value="-1">Choose</option>
    <option value="child">test2</option>
</select>

JavaScript

JavaScript

$("#formid").validate({
    select: {
        required: function(element) {
            if ($("#select").val() == '-1') {
                return false;
            } else {
                return true;
            }
        }
    }
}); 

How do I get this working?

我如何让这个工作?

回答by 3dgoo

A simple way to fix this problem is to give the non valid optionthe value of "". Then simply call validate on your form and it will not submit when "Choose"is selected.

解决此问题的一个简单方法是为无效option"". 然后只需在您的表单上调用validate,它就不会在"Choose"被选中时提交。

HTML

HTML

<form id="formid">
    <select name="select" class="required">
        <option value="">Choose</option>
        <option value="child">test2</option>
    </select>

    <input type="submit" />
</form>?

JavaScript

JavaScript

$("#formid").validate(); ?

Demo

演示

回答by Cameron Bielstein

Although this probably works with some of the aforementioned methods,if you're looking to use a custom validation function, you should use addMethod as documented here: http://docs.jquery.com/Plugins/Validation/Validator/addMethod

尽管这可能适用于上述某些方法,但如果您希望使用自定义验证功能,则应使用此处记录的 addMethod:http: //docs.jquery.com/Plugins/Validation/Validator/addMethod

So you would first add the method through something like

所以你会首先通过类似的东西添加方法

$.validator.addMethod("requiredSelect", function(element) {
                return ( $("#select").val() !='-1' );
            }, "You must select an option.");

Then simply assign the validator with

然后简单地分配验证器

$("#formid").validate({
  rules: {
    select: { requiredSelect : true }
  }
});

回答by froston

There is missing nameattribute in your selectelement. In my case that was the issue since the jQuery Validatation Plugin looks for the namenot idwhile validating.

丢失name你的属性select元素。在我的情况下,这是问题,因为 jQuery 验证插件在验证时查找namenot id

回答by Anantha Sharma

instead of:

代替:

$("#select").val()

$("#select").val()

try:

尝试:

$("#select :selected").val()

$("#select :selected").val()

$("#select").val()returns all the option values instead of the selected one.

$("#select").val()返回所有选项值而不是选定的值。

Here, my assumption is that you want to check if the user has chosen the option -1 when the control report-crimeis validated.

在这里,我的假设是您要检查用户是否在report-crime验证控件时选择了选项 -1 。

回答by aifarfa

by default

默认情况下

<option value="">Choose</option>

<option value="">Choose</option>

works with

required: true

required: true