[jQuery] Vérifier si au moins une case à cocher ou un bouton radio est coché
Source : CHECK IF ANY CHECKBOXES ARE SELECTED IN JQUERY
1 2 3 4 5 6 7 8 9 10 11 12 |
function checkChecked(formname) { var anyBoxesChecked = false; $('#' + formname + ' input[type="checkbox"]').each(function() { if ($(this).is(":checked")) { anyBoxesChecked = true; } }); if (anyBoxesChecked == false) { //Do something } } |
Cas un peu plus complexe :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Vérifier qu'au moins une case à cocher est active dans chaque catégorie de filtres // et afficher un témoin en fonction function checkIfAnyBoxIsChecked(){ var anyBoxIsChecked = false; $('ul.Filters-facets__list').each(function(){ if ($(this).find('input[type="checkbox"]').is(':checked')) { anyBoxIsChecked = true; $(this).closest('.facet-nav-item').addClass('has-active-facet'); } }); if (anyBoxIsChecked == false) {} } checkIfAnyBoxIsChecked(); |
Codes HTML et SASS purement indicatifs (ne… Lire la suite »[jQuery] Vérifier si au moins une case à cocher ou un bouton radio est coché