jQuery Validation Plugin: Validating Checkboxes with Different Names

前端 未结 3 1830
鱼传尺愫
鱼传尺愫 2021-02-03 16:38

I have a set of 4 checkboxes, all with different names, and require that at least 1 is checked.

I have set the class on all of them to \'require-one\'.



        
3条回答
  •  滥情空心
    2021-02-03 16:54

    Swatting a fly with a rocket launcher?

    $('.require-one:checked').size() == 0;
    

    Apply the rule on any one of the checkboxes using it's name. For the form to be valid, this checkbox must pass validations. For this checkbox to be valid, at least one of the 4 checkboxes must be checked.

    $("#itemForm").validate({
    rules: { 
        'check1': {  
            required: {
                depends: function(element) {
                    return $('.require-one:checked').size() == 0;
                }
            } 
        } 
    }
    });
    

    Updated 2:

    http://jsfiddle.net/MkPtP/1/

提交回复
热议问题