jQuery Validation at least one checkbox is selected

后端 未结 5 1792
鱼传尺愫
鱼传尺愫 2021-01-15 02:05

I have the following piece of html code for form checkboxes

5条回答
  •  长情又很酷
    2021-01-15 03:02

    You will have to change the name attributes of the checkboxes, and add a rule for it with the minlength set to 1.

    You can after show the custom message and append it to #error_msg3 with :

    if(element.attr("name") == "check") error.appendTo("#error_msg3")
    

    EDIT

    $("input[type='checkbox'][name='check']").change(function() {
        if ($("input[type='checkbox'][name='check']:checked").length){
            $(this).valid()
        }
    })
    

    EDIT 2

    I've updated the Fiddle, now the message toggles correctly when you have at least one checkbox checked or not.

    $("input[type='checkbox'][name='check']").change(function() {
        $('#submitDetails').valid();
    });
    


    Fiddle here

提交回复
热议问题