Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
¿Cómo cuento cuántas casillas de verificación están seleccionadas en una página con jQuery?

Quiero contar cuántas casillas de verificación en mi página están seleccionadas usando jQuery. He escrito el siguiente código:

 var numberOfCheckboxesSelected = 0; $(':checkbox').each(function(checkbox) { if (checkbox.attr('checked')) numberOfCheckboxesSelected++; });

Sin embargo, mi código falla con el mensaje "El objeto no admite esta propiedad o método" en la tercera línea.

¿Cómo cuento cuántas casillas de verificación están seleccionadas en mi página?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

jQuery admite el pseudoselector :checked .

 var n = $("input:checked").length;

Esto funcionará tanto para los botones de opción como para las casillas de verificación. Si solo desea casillas de verificación, pero también tiene botones de opción en la página:

 var n = $("input:checkbox:checked").length;
over 4 years ago · Santiago Trujillo Report

0

Pruebe esto (para evitar contar los botones de opción seleccionados):

 var numberOfCheckboxesSelected = $('input[type=checkbox]:checked').length;
over 4 years ago · Santiago Trujillo Report

0

El primer argumento pasado a la devolución de llamada en each es el índice, no el elemento. Deberías usar this o un segundo argumento:

 $(':checkbox').each(function(idx, checkbox) { if (checkbox.attr('checked')) numberOfCheckboxesSelected++; });

Además, la forma más fácil de hacer esto es seleccionar solo los elementos marcados con el selector :checked y luego verificar la propiedad de length de la selección:

 var numberOfCheckboxesSelected = $('input:checkbox:checked').length;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!