0

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][]" />option1

<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.

1
  • I found this, I hoe its useful to you. Commented Apr 8, 2011 at 20:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Drackir. But if the ID is passed in the query string like: ?attachment_id=29 how can I capture that variable. Thanks.
@José Pablo Orozco Marín: I'm not sure I understand completely. Can you not populate it with a server-side language like php or ASP.Net? Or is it loaded using AJAX?
nope, is wordpress, and the ID is passed by query string: attachment_id=29 where 29 is an attachment/post ID. And im using Jquery to validate those checkboxes, but I cdont know how to capture the query string with Jquery.
I think im going to try: github.com/allmarkedup/jQuery-URL-Parser Thank you Drackir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.