IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

restreindre choix dans cases à cocher


Sujet :

JavaScript

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2007
    Messages
    18
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 18
    Points : 17
    Points
    17
    Par défaut restreindre choix dans cases à cocher
    bonsoir,
    Comment gérer les choix multiples de cases à cocher (checkbox) d'un formulaire, de manière à limiter le nombre de coches possible pour l'utilisateur.
    (exemple : limiter à 3 dans un groupe de 10 checkboxs) ?

    - soit par un paramétrage de checkbox (si ça existe)
    - soit par un script javascript qui empèche de cocher une 4 ème case.

  2. #2
    Expert éminent sénior
    Avatar de Auteur
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    7 650
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 7 650
    Points : 11 142
    Points
    11 142
    Par défaut
    bonjour,

    voici un exemple avec 4 cases à cocher au maximum (le nombre de cases maximum à cocher est défini dans la variable nbMaxChk):
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
     
    <head>
     
    <title></title>
     
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Language" content="fr" />
     
    <script type="text/javascript">
    <!--  
    var nbMaxChk = 4; // nombre maximum de cases à cocher
     
    var compteChk = 0; // nombre de cases cochées
    function cocher(objet)
    {
      if (objet.checked)
      {
        if (compteChk==nbMaxChk)
          objet.checked = false;
        else
          compteChk++;
      }
      else
        compteChk--;
        
    }
    //-->
    </script>
     
    </head>
     
    <body>
     
    <div>
      <input type="checkbox" onclick="cocher(this)" id="c1" /><label for="c1">choix 1</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c2" /><label for="c2">choix 2</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c3" /><label for="c3">choix 3</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c4" /><label for="c4">choix 4</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c5" /><label for="c5">choix 5</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c6" /><label for="c6">choix 6</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c7" /><label for="c7">choix 7</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c8" /><label for="c8">choix 8</label>
    </div>
     
    </body>
     
    </html>

    mais ce script a un bug : si tu coches des cases puis si tu recharges la page, tu pourras cocher plus de 4 cases. Il faudra donc améliorer ce point


    [Edit]
    Voici un exemple qui corrige le bug ci-dessus :
    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
     
    <head>
     
    <title></title>
     
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Language" content="fr" />
     
    <script type="text/javascript">
    <!--  
    var nbMaxChk = 4;
     
    var compteChk;
     
    function initVar()
    {
      var i, n;
      var tabInput;
      
      compteChk = 0;
      
      tabInput = document.getElementById("conteneurChk").getElementsByTagName("input");
      n = tabInput.length;
      
      for (i=0; i<n; i++)
      {
        if (tabInput[i].type.toLowerCase()=="checkbox" && tabInput[i].checked)
          compteChk++;
      }
    }
     
    function cocher(objet)
    {
      if (objet.checked)
      {
        if (compteChk==nbMaxChk)
          objet.checked = false;
        else
          compteChk++;
      }
      else
        compteChk--;
        
    }
    //-->
    </script>
     
    </head>
     
    <body onload="initVar()">
     
    <div id="conteneurChk">
      <input type="checkbox" onclick="cocher(this)" id="c1" /><label for="c1">choix 1</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c2" /><label for="c2">choix 2</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c3" /><label for="c3">choix 3</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c4" /><label for="c4">choix 4</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c5" /><label for="c5">choix 5</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c6" /><label for="c6">choix 6</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c7" /><label for="c7">choix 7</label><br/>
      <input type="checkbox" onclick="cocher(this)" id="c8" /><label for="c8">choix 8</label>
    </div>
     
    </body>
     
    </html>
    J'appelle la fonction initVar() lors du chargement de la page pour compter le nombre de chekbox déjà cochées.

    [/edit]

  3. #3
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 663
    Points
    66 663
    Billets dans le blog
    1
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
     
    <head>
     
    <title></title>
     
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta http-equiv="Content-Language" content="fr" />
     
    <script type="text/javascript">
    <!--  
    var nbMaxChk = 4;
     
    var compteChk;
     
    function initVar()
    {
      var i, n;
      var tabChecks;
     
      compteChk = 0;
     
      tabChecks = document.getElementsByName("check")
      n = tabChecks.length;
     
      for (i=0; i<n; i++)
      {
        tabChecks[i].checked=false
      }
    }
     
    function cocher(objet)
    {
      if (objet.checked)
      {
        if (compteChk==nbMaxChk)
          objet.checked = false;
        else
          compteChk++;
      }
      else
        compteChk--;
     
    }
    //-->
    </script>
     
    </head>
     
    <body onload="initVar()">
     
    <div id="conteneurChk">
      <input type="checkbox" name='check' onclick="cocher(this)" id="c1" /><label for="c1">choix 1</label><br/>
      <input type="checkbox" name='check' onclick="cocher(this)" id="c2"   /><label for="c2">choix 2</label><br/>
      <input type="checkbox" name='check' onclick="cocher(this)" id="c3"  /><label for="c3">choix 3</label><br/>
      <input type="checkbox" name='check' onclick="cocher(this)" id="c4" /><label for="c4">choix 4</label><br/>
      <input type="checkbox" name='check' onclick="cocher(this)" id="c5"  /><label for="c5">choix 5</label><br/>
      <input type="checkbox" name='check' onclick="cocher(this)" id="c6"   /><label for="c6">choix 6</label><br/>
      <input type="checkbox" name='check' onclick="cocher(this)" id="c7"  /><label for="c7">choix 7</label><br/>
      <input type="checkbox" name='check' onclick="cocher(this)" id="c8"   /><label for="c8">choix 8</label>
    </div>
     
    </body>
     
    </html>

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2007
    Messages
    18
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 18
    Points : 17
    Points
    17
    Par défaut
    merci.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] Récupérer info dans case à cocher
    Par Pompaero dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 16/04/2013, 10h09
  2. [AC-2007] Choix multiples (case à cocher)
    Par ulrichv dans le forum IHM
    Réponses: 8
    Dernier message: 21/12/2009, 15h02
  3. Réponses: 7
    Dernier message: 30/07/2008, 18h51
  4. Impression suite à un choix par case à cocher
    Par doudoufly dans le forum VBA Word
    Réponses: 1
    Dernier message: 02/07/2008, 23h05
  5. Case à cocher dans une requête
    Par kloss dans le forum Access
    Réponses: 6
    Dernier message: 14/10/2004, 11h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo