简体   繁体   中英

How to count the number of checked checkboxes on blur event

I am trying to count the number of checked checkboxes on blur event. But it looks like the number of blur events is being counted. How to count the number of checked checkboxes in array?

 const requiredBoxes = [...document.querySelectorAll('.js-form-checkbox')]; requiredBoxes.forEach(function (input) { input.addEventListener('blur', () => { console.log(requiredBoxes.filter(item => item.checked).length); }); });
 <input type="checkbox" class="js-form-checkbox" /> <input type="checkbox" class="js-form-checkbox" /> <input type="checkbox" class="js-form-checkbox" /> <input type="checkbox" class="js-form-checkbox" />

The clicks are not being counted, when you press the checkbox, you don't see the blur event immediately. You can check by checking the checkbox and clicking elsewhere but checkboxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM