Controler des checkbox

Fermé
Catherine - 3 nov. 2005 à 14:27
mgwilow Messages postés 2 Date d'inscription lundi 19 janvier 2015 Statut Membre Dernière intervention 19 janvier 2015 - 19 janv. 2015 à 19:48
Bonjour à tous,

j'ai un formulaire html avec 19 checkbox. Voici ma question, je voudrais que le visiteur une fois qu'il a coché 6 checkbox au choix que si il en coche une 7 eme qu'il y ai un message qui lui dise qu'il ne peut en cocher + que 6
Pouvez vous m'aider à trouver la solution

Très grand merci

4 réponses

Utilisateur anonyme
3 nov. 2005 à 15:10
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
	<title>Untitled</title>
	<script>
	function checkSelect(elmId){
	  maxOptionsSelect = 7;
	  form = document.all("detailform");
	  inputs = form.getElementsByTagName("input");
	  count = 0;
	  for(i=0 ; i<inputs.length ; i++){
	    if(inputs[i].type=="checkbox" && inputs[i].checked==true){
		  count++;
		}
	  }
	  
	  if(count > maxOptionsSelect){
	  	document.all(elmId).checked=false;
	  	alert("Vous ne pouvez sélectionner que maximum " + maxOptionsSelect + " options");
		return false;
	  }
	  return false;
	}
	</script>
</head>

<body>

<form name="detailform">
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_1"/>a<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_2"/>b<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_3"/>c<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_4"/>d<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_5"/>e<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_6"/>f<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_7"/>g<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_8"/>h<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_9"/>i<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_10"/>j<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_11"/>k<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_12"/>l<br/>
	  <input type="checkbox" onclick="javascript:checkSelect(this.id);" id="cb_13"/>m<br/>	  	  	  		  	  	  	  	  	  
</form>

</body>
</html>


;-)
HackTrack
15
Génial ;o))

Un grand merci pour le code, je le tiens bien au chaud pour le tester

Merci mille fois ;o))

Catherine
0
wiwimagique Messages postés 481 Date d'inscription mardi 21 janvier 2003 Statut Membre Dernière intervention 20 avril 2006 108
3 nov. 2005 à 15:16
Moi, je l'ai juste pensé dans ma tête.

C'est parce que je suis flemmard :-D
0
Utilisateur anonyme > wiwimagique Messages postés 481 Date d'inscription mardi 21 janvier 2003 Statut Membre Dernière intervention 20 avril 2006
3 nov. 2005 à 15:25
Pour dire ça autant se taire...

;-)
HackTrack
0
catherine > Utilisateur anonyme
3 nov. 2005 à 15:35
Ca marche ;o)))
Je viens de le tester sur mon form, super génial ;o))

Merci encore de ton aide
0
Utilisateur anonyme > catherine
3 nov. 2005 à 20:40
Pas de quoi

A ton service...

;-)
HackTrack
0
wiwimagique Messages postés 481 Date d'inscription mardi 21 janvier 2003 Statut Membre Dernière intervention 20 avril 2006 108
3 nov. 2005 à 14:33
il faut faire ça en javascript

à chaque fois q'un utilisateur coche ou décoche un checkbox ( onclick ) tu appelles une fonction

la fonction va parcourir la totalité des checkbox et compter combien sont cochés (.checked = true)
si ça dépasse 6, tu fais ton alert('message d erreur') et tu décoches la box qui vient d'etre modifiée.

1
Bonjour ,

je n'y connais absolument rien en programmation, et c'est du charabia pour moi, merci de ton aide en tout cas mais peux tu m'écrire le code pour y voir + clair ?
Ce serait hyper gentil de ta part
merci
0
S.V.I.N.K.E.L.S
3 nov. 2005 à 15:28
Pour dire ça autant se taire...

Lool t'as vu ton message la ! :D

S.V.I.N.K.E.L.S
-->on se gave d'orge d'houblon et de malt a wishy: C real killer
1
mgwilow Messages postés 2 Date d'inscription lundi 19 janvier 2015 Statut Membre Dernière intervention 19 janvier 2015 1
19 janv. 2015 à 19:48
bonjour, je n'y connais pas grand chose en javascript mais comment faire pour ne plus permettre de cocher d'autre case après le nombre de 6 dans l'exemple?

le code fonctionne nickel merci bcp.
1

Discussions similaires