jQuery验证至少一个复选框 [英] jquery validation for at least one checkbox

查看:78
本文介绍了jQuery验证至少一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确的,此主题之前已经在至少一个复选框的验证中进行了讨论(以及相关的演示网站 http://jsfiddle.net/RGUTv/).所以我复制了解决方案,但它似乎不起作用,我也弄不清原因.控制台显示在validate()调用之后根本不触发已实现的功能.我认为该错误只能是我的html代码,但是该错误的具体含义是什么?

Correct, this topic has been discussed before like at validation for at least one checkbox (and related demo site http://jsfiddle.net/RGUTv/). So I copied the solution but it doesnt seem to work and I cant figure out why. The console shows the implemented functions are not triggered at all after a validate() call. I assume the error can only my html code, but what specific is the error?

有什么建议吗?

谢谢.

HTML代码:

<input type="checkbox" id="check0" name="productselection0" value="productselected0"  class="require-one">

<input type="checkbox" id="check1" name="productselection1" value="productselected1"  class="require-one">

<input type="checkbox" id="check2" name="productselection2" value="productselected2"  class="require-one">

JavaScript代码(解决方案上的一对一副本,位于至少一个复选框的验证):

Javascript code (1 on 1 copy from solution at validation for at least one checkbox):

<script>
    $(document).ready(function() {

        $.validator.addMethod('require-one', function(value) {
            console.log("addMethod triggered");
            return $('.require-one:checked').size() > 0;
        }, 'Please check at least one box.');

        var checkboxes = $('.require-one');
        var checkbox_names = $.map(checkboxes, function(e, i) {
            return $(e).attr("name")
        }).join(" ");

        $("#myForm").validate({
            groups: {
                checks: checkbox_names
            },
            errorPlacement: function(error, element) {
                if (element.attr("type") == "checkbox")
                    error.insertAfter(checkboxes.last());
                else error.insertAfter(element);
            }       
        }); //validate()

}); //function
</script> 

推荐答案

additional-method.js文件,有一种名为require_from_group的方法,该方法将执行您描述的操作...从选定的一组对象中指定数量的输入元素required输入元素. (确保使用已修复错误的最新版本(1.11.1))

In the additional-method.js file, there is a method called require_from_group that will do what you describe... make a specified number input element(s) required out of a selected group of input elements. (make sure to use the latest version (1.11.1) which fixed a bug)

$(document).ready(function () {

    $('#myform').validate({
        rules: {
            productselection0: {
                require_from_group: [1, '.require-one']
            },
            productselection1: {
                require_from_group: [1, '.require-one']
            },
            productselection2: {
                require_from_group: [1, '.require-one']
            }
        },
        groups: {
            checks: "productselection0 productselection2 productselection3"
        }
    });

});

正在运行的演示: http://jsfiddle.net/Fr4y4/

这篇关于jQuery验证至少一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆