Count selected checkbox

Asked

Viewed 524 times

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 the code $('input[type="checkbox"]:checked').length 1 is displayed, and no checkbox is selected. If I select only one, 2 is displayed and so on. How to solve this problem?

  • Give a console.log($('input[type="checkbox"]:checked')) check which checkbox are listed, make sure there are no hidden.

  • It looks good here: http://jsfiddle.net/t9t5cz86/ you can adapt jsFiddle with your code to see if we find the problem?

  • @Kaduamaral did this, and look what appeared: http://postimg.org/image/mdx7uqo1f/. Nowhere have I found this code.

  • @Sergio my code is like this: I have two radio with the options PUBLIC and PRIVATE. If the user clicks PRIVATE, my application (in Asp.net mvc) searches for a list of SECTORS and I display the sectors on the screen for the user to select. I’ll try to put the code in the url for you to see

  • 1

    @Zackmorgan do you use any framework? This checkbox has something to do with menu. The cause of the "being an extra" number is because of this checkbox, there is nothing wrong with your code. Either you have to remove it or use another logic.

  • @Kaduamaral use several, for css, javascript. This application is being developed in Asp.net mvc. I will perform a test with all frameworks in the project.

Show 1 more comment

1 answer

4


The problem is that you are selecting all the checkbox from the page, and may give conflict with other checkbox groups.

Use a class or attribute to group them:

<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
  • 100% that way. I’ll leave to search which plugin did that. Thanks for the help friend!!! Abs

  • The title attribute has something to do with the @Zackmorgan menu.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.