2

My HTMl:

<input type="checkbox" name="subjects[0]" class="checkbox_array" value="1"  />
<input type="checkbox" name="subjects[1]" class="checkbox_array"  value="1"  />
<input type="checkbox" name="subjects[2]" class="checkbox_array" value="1"  />
<input type="checkbox" name="subjects[3]" class="checkbox_array" value="1"  />
<input type="button" id="ss" />

jquery:

$("#ss").click(function(){
    if($('input[name=subjects\\[\\]]:checked').length<=0)
    {
        alert("No radio checked")
    }
});

its not working for me... using class if($('.checkbox_array:checked').length<=0) it work that easy but how can i do with input name selector

5
  • 1
    I think you can/should remove the number from inside the array brackets -- name="subjects[]" -- if you're using PHP server-side, it will build an array for you automatically. Commented Oct 24, 2013 at 13:17
  • Why do you not want to use the class selector? It's more semantic, and probably quicker. Commented Oct 24, 2013 at 13:18
  • have some many array like this, for code simply used name because if we use class we need to give for all @RoryMcCrossan Commented Oct 24, 2013 at 13:24
  • You could surround them in a container, then the class selector would work for all instances, rather than having to change the 'name starts with' selector for every group. Commented Oct 24, 2013 at 13:25
  • this is a cms project so if any others edits without coding knowledge they will get confuse so i used numbers, so they can know the groups and also they won't miss the name @Blazemonger Commented Oct 24, 2013 at 13:26

2 Answers 2

6
($('input[name*="subjects"]:checked').length<=0)

Will work...

Doc: http://api.jquery.com/attribute-contains-selector/

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

Comments

5

You can do this using the Attribute Starts With Selector:

$("#ss").click(function () {
    if ($('input[name^=subjects]:checked').length <= 0) {
        alert("No radio checked")
    }
});

Demo: Fiddle

Comments

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.