par
AB » 21 févr. 2009, 14:12
Donc en termes concrets on peux faire comme ça
Code : Tout sélectionner
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans titre</title>
<script type="text/javascript">
function checkall(nomcheck,init)
{
this.i = (typeof this.i == 'number')? this.i : parseInt(init);
var id = nomcheck+i;
if (this.box = document.getElementById(id))
{
this.box.checked="checked";
this.i++;
checkall(nomcheck,init);
}
}
</script>
</head>
<body>
<div>
<form action ="#" method = "post">
<p>
<input type="checkbox" id = "check1" name = "groupe[]" />
<input type="checkbox" id = "check2" name = "groupe[]" />
<input type="checkbox" id = "check3" name = "groupe[]" />
<input type="checkbox" id = "check4" name = "groupe[]" />
<input type = "button" onclick = "checkall('check',1)" value = "Coche tout" />
</p>
</form>
</div>
</body>
</html>
Le premier paramètre de checkall('check',1) indique le nom des id.
Le deuxième paramètre (ici = 1) indique le numéro initial de départ. Si tu commençais à partir de check0, il faudrait appeler la fonction checkall('check',0)
Donc en termes concrets on peux faire comme ça
[code]<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans titre</title>
<script type="text/javascript">
function checkall(nomcheck,init)
{
this.i = (typeof this.i == 'number')? this.i : parseInt(init);
var id = nomcheck+i;
if (this.box = document.getElementById(id))
{
this.box.checked="checked";
this.i++;
checkall(nomcheck,init);
}
}
</script>
</head>
<body>
<div>
<form action ="#" method = "post">
<p>
<input type="checkbox" id = "check1" name = "groupe[]" />
<input type="checkbox" id = "check2" name = "groupe[]" />
<input type="checkbox" id = "check3" name = "groupe[]" />
<input type="checkbox" id = "check4" name = "groupe[]" />
<input type = "button" onclick = "checkall('check',1)" value = "Coche tout" />
</p>
</form>
</div>
</body>
</html>[/code]
Le premier paramètre de checkall('check',1) indique le nom des id.
Le deuxième paramètre (ici = 1) indique le numéro initial de départ. Si tu commençais à partir de check0, il faudrait appeler la fonction checkall('check',0)