jQuery: количество выбранных checkbox и сбросить

Решение, как вывести счётчик отмеченных checkbox и обнуление значений по кнопке «Сбросить» на jQuery
Решение:
HTML
<div>
  (Выбрано: <span id="count"></span>)
  <a id="invert" href="#">Сбросить</a>
</div>

<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
JS
var count = 0;
$(function() {
    count = $('input[type=checkbox]:checked').length;
    displayCount();  

    $('input[type=checkbox]').bind('click' , function(e, a) {   
         if (this.checked) {
              count += a ? -1 : 1;
         } else {
              count += a ? 1 : -1;
         }
         displayCount();
    });
    $('#invert').click(function(e) {    
         $('#count').text(0);
         $('input[type=checkbox]').removeAttr("checked");
         count = 0;
    });
});
function displayCount() {
    $('#count').text(count);
}


Похожие решения:
Изменено: 14 03 2020
Просмотров: 1404