Jak liczyć pola wyboru za pomocą jQuery?
Mam mnóstwo checkboxów, które są zaznaczone (checked="checked"
) lub odznaczone.
Chciałbym uzyskać liczbę wszystkich checkboxów, niezaznaczonych i zaznaczonych.
Z pole wyboru mam na myśli <input type="checkbox" />
.
Jak to zrobić z jQuery? Z góry dzięki!
38
Author: Nitin Bhargava, 2011-11-04
4 answers
Możesz zrobić:
var numberOfChecked = $('input:checkbox:checked').length;
var totalCheckboxes = $('input:checkbox').length;
var numberNotChecked = totalCheckboxes - numberOfChecked;
EDIT
Lub nawet proste
var numberNotChecked = $('input:checkbox:not(":checked")').length;
123
Author: Nicola Peluchetti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-17 12:34:42
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-17 12:34:42
Następujący kod zadziałał dla mnie.
$('input[name="chkGender[]"]:checked').length;
10
Author: Nguyễn Thành Bồi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-28 10:36:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-28 10:36:43
Załóżmy, że masz wiersz tr z wieloma checkboxami i chcesz liczyć tylko wtedy, gdy pierwsze pole jest zaznaczone.
Możesz to zrobić, podając klasę do pierwszego checkboxa
Na przykład class='mycxk'
i możesz to policzyć używając filtra, jak to
$('.mycxk').filter(':checked').length
6
Author: Samuel Aiala Ferreira,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-13 12:06:13
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-13 12:06:13
Istnieje wiele metod, aby to zrobić:
Metoda 1:
alert($('.checkbox_class_here:checked').size());
Metoda 2:
alert($('input[name=checkbox_name]').attr('checked'));
Metoda 3:
alert($(":checkbox:checked").length);
2
Author: Hasnain Mehmood,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-09 07:16:13
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-09 07:16:13