Vérifier le nombre des checkbox cochés

kimyoo Messages postés 43 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:
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

  1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830
     
    Bonjour,

    Désolé.. mais je ne comprend pas ta question...
    pourrais tu être un peu plus explicite ?
    0
    1. kimyoo Messages postés 43 Statut Membre
       
      OK
      j'ai une liste des checkbox et il faut cocher juste 4 parmi cette liste mais quand il coché 3, 2 ou rien, le but est de retourner les 4 checkbox coché par défaut
      c'est tout.
      j'espére que tu comprend mon idée
      désolé pour mon français :)
      0
      1. jordane45 Messages postés 30426 Date d'inscription   Statut Modérateur Dernière intervention   4 830 > kimyoo Messages postés 43 Statut Membre
         
        - Comment sais tu lesquelles sont à cocher par défaut ?
        - Peux tu nous montrer le code HTML qui va avec ton code js ?
        0
  2. kimyoo Messages postés 43 Statut Membre
     
    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>
    0
  3. Pitet Messages postés 2845 Statut Membre 530
     
    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
    0