Vérifier le nombre des checkbox cochés
kimyoo
Messages postés
43
Statut
Membre
-
Pitet Messages postés 2845 Statut Membre -
Pitet Messages postés 2845 Statut Membre -
Bonjour,
j'ai créé une fonction jquery pour cochéé juste 4 checkbox ni plus ni moins
comment faire quand le nombre de checkbox < 4, il me retourne les 4 derniers checkbox cochéé ?
voici mon code:
vous avez une idée?
Merci d'avance
j'ai créé une fonction jquery pour cochéé juste 4 checkbox ni plus ni moins
comment faire quand le nombre de checkbox < 4, il me retourne les 4 derniers checkbox cochéé ?
voici mon code:
if ($('input[type=checkbox]:checked').length > 4) {
$(this).prop('checked', false);
alert("seulement 4 catégories peuvent être affichés");
} else if ($('input[type=checkbox]:checked').length < 4) {
$(this).prop('checked', false);
alert("c'sest obligatoire de sélectionner 4 catégorie");
}
vous avez une idée?
Merci d'avance
3 réponses
-
Bonjour,
Désolé.. mais je ne comprend pas ta question...
pourrais tu être un peu plus explicite ?
-
Ok
Merci jordane45 pour l'intéret<table class="table table-striped"> <thead> <tr> <th>#</th> <th>Etat</th> <th>Lib</th> <th></th> </tr> </thead> <tbody> <?php foreach($parent as $value){?> <tr> <td><?php echo $value->cat_id;?></td> <td> <input type="checkbox" class="check" id="<?php echo $value->cat_id;?>" data-on="1" data-off="0" <?php if($value->cat_active == 1){echo "checked";}?> /></td> <td> <?php echo $value->cat_lib;?><a href="<?php echo site_url('category/'.$value->cat_id);?>"> </a></td> <td> <div class="btn-group"> <a href="<?php echo site_url('category/edit/'.$value->cat_id);?>" class="btn btn-info btn-mini"> <i class="fa fa-eye"></i> Modifier </a> <a href="<?php echo site_url('category/delete/?act=del&id='.$value->cat_id);?>" onclick="return confirm('Etes-vous sur de vouloir supprimer cet Admin ?');" class="btn btn-danger btn-mini" ><i class="fa fa-trash-o"></i> supp.</a> </div> </td> </tr> <?php }?> </tbody> </table> -
Salut,
Voici un début de solution :<div class="checkbox-message">Vous devez choisir 4 catégories</div> <label><input type="checkbox" name="checkbox1" class="cb4choix" /> choix 1</label> <label><input type="checkbox" name="checkbox2" class="cb4choix" /> choix 2</label> <label><input type="checkbox" name="checkbox3" class="cb4choix" /> choix 3</label> <label><input type="checkbox" name="checkbox4" class="cb4choix" /> choix 4</label> <label><input type="checkbox" name="checkbox5" class="cb4choix" /> choix 5</label> <label><input type="checkbox" name="checkbox6" class="cb4choix" /> choix 6</label>
/* requiert jQuery */ $(function() { $('.cb4choix').change(function() { if ($('.cb4choix:checked').length < 4) { $('.checkbox-message').html('Vous devez choisir 4 catégories'); } else if ($('.cb4choix:checked').length > 4) { $(this).prop('checked', false); } else { $('.checkbox-message').html('Vous avez choisi 4 catégories'); } }); });
Bonne journée