开发者

Limit array checkboxes

开发者 https://www.devze.com 2023-02-23 07:16 出处:网络
If you want to prevent a user to mark more than, for example, 4 checkboxes, you can use: $(\"input[type=checkbox][name=subculture]\").click(function() {

If you want to prevent a user to mark more than, for example, 4 checkboxes, you can use:

$("input[type=checkbox][name=subculture]").click(function() {

    var bol = $("input[type=checkbox][name=subculture]:checked").length >= 4;     
    $("input[type=checkbox][name=subculture]").not(":checked").attr("disabled",bol);

});

But if my checkboxes are an Array like:

<input type="checkbox" value="option1" id="attachments[29][subculture][]" name="attachments[29][subculture][]" />op开发者_如何学Gotion1

<input type="checkbox" value="option2" id="attachments[29][subculture][]" name="attachments[29][subculture][]" />option2

<input type="checkbox" value="option3" id="attachments[29][subculture][]" name="attachments[29][subculture][]" />option3

...

How can I fix the above Jquery code to validate something like that?

where 29 is the ID of the actual post edited.

Thanks in advance.


You're supposed to use double quotes with those types of selectors. Doing it that way will allow you to specify the names you want. See below or my jsFiddle example:

$("input[type=checkbox][name=\"attachments[29][subculture][]\"]").click(function() {
    var bol = $("input[type=checkbox][name=\"attachments[29][subculture][]\"]:checked").length >= 4;     
    $("input[type=checkbox][name=\"attachments[29][subculture][]\"]").not(":checked").attr("disabled",bol);
});

The other option you have is to add a class to each input and then use that as the selector.

0

精彩评论

暂无评论...
验证码 换一张
取 消