Count selected checkboxes

1

I'm having trouble counting the checkboxes that are selected, maybe for the following reason, I have a radio that is also selected. Whenever I use code $('input[type="checkbox"]:checked').length is given a value of 1, and no checkbox is selected. If I select only one, the value 2 is displayed and so on. How do I resolve this issue?

    
asked by anonymous 14.07.2015 / 21:12

1 answer

3

The problem is that you are selecting all the checkboxes on the page, and can conflict with other checkbox groups.

Use a class or attribute to group them together:

<input type="checkbox" class="setor" [...]>

$('input.setor[type="checkbox"]:checked').length

Or

<input type="checkbox" data-context="setor" [...]>

$('input[type="checkbox"][data-context="setor"]:checked').length
    
14.07.2015 / 21:36