Bouton cocher/décocher

Résolu/Fermé
isa-- Messages postés 77 Date d'inscription lundi 16 septembre 2013 Statut Membre Dernière intervention 11 mars 2020 - Modifié le 22 nov. 2018 à 22:40
isa-- Messages postés 77 Date d'inscription lundi 16 septembre 2013 Statut Membre Dernière intervention 11 mars 2020 - 23 nov. 2018 à 14:48
Bonjour à tous,
J'ai besoin d'un script pour cocher et décocher des checkboxes.
N'étant pas du tout bonne en Javascript, j'ai glané un code qui fonctionne parfaitement, jusqu'à ce que je mette tout cela dans un tableau bien propre, et là, ça me marche plus du tout.
J'imagine que c'est à cause du div div_chck, qui du coup se termine à chaque </td>...
Je ne sais pas comment m'en sortir !
Quelqu'un pour m'aider ?
Merci d'avance
Ci dessous mon code qui fonctionne :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" > 
<head> 
<title>Relance litiges</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
</head> 
<body> 
<script type="text/javascript"> function GereChkbox(conteneur, a_faire) { 
var blnEtat=null; 
var Chckbox = document.getElementById(conteneur).firstChild; 
while (Chckbox!=null) { if (Chckbox.nodeName=="INPUT") 
if (Chckbox.getAttribute("type")=="checkbox") { blnEtat = (a_faire=='0') ? 
false : (a_faire=='1') ? true : (document.getElementById(Chckbox.getAttribute("id")).checked) ? 
false : true; document.getElementById(Chckbox.getAttribute("id")).checked=blnEtat; } Chckbox = Chckbox.nextSibling; } } 
</script> 
<form> 
<br/> 

<input type="button" value="Tout cocher" onClick="GereChkbox('div_chck','1');">
<input type="button" value="Tout décocher" onClick="GereChkbox('div_chck','0');">

<?php
echo "<div id=\"div_chck\">";
include("../connexion.php");
$req = "SELECT * FROM `retours` where `lr`='L' AND `date_reclam`!='0000-00-00' AND `cloture`='0000-00-00' and `erreur`!='TRANSPORT' ORDER BY cial,fourn";
$res = $conn->query($req); 
$nb=mysqli_num_rows($res);
$i=0;
while ($data = mysqli_fetch_array($res)) {
$i++;
$id=$data['id'];
//echo "<tr><td>";
echo "<input type=\"checkbox\" name=\"checkbox".$i."\" id=\"checkbox".$i."\" value=\"".$i."\">".$id."<br>";
//echo "</td></tr>";
}
?>
</div>
</table>
</form> 
<noscript> <a href="http://www.editeurjavascript.com/">ajax</a> </noscript> 
</body> </html> 

Configuration: Windows / Firefox 63.0

2 réponses

jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
Modifié le 22 nov. 2018 à 22:48
Bonjour,

Déjà, remplace ton code par celui la
echo "<input type='checkbox' name='checkbox[".$i."]' id='checkbox".$i."' value='".$i."' class='chkbx'>".$id."<br>";

comme tu peux le voir, j'ai ajouté une class (entre autres choses...)
On va donc pouvoir se servir de la class comme "selecteur"
function GereChkbox(className,etat){
  var clist=document.getElementsByClassName(className);
  for (var i = 0; i < clist.length; ++i) {
    clist[i].checked = etat==1 ? "checked" : false; 
  }
}


et pour les boutons
<input type="button" value="Tout cocher" onClick="GereChkbox('chkbx','1');">
<input type="button" value="Tout décocher" onClick="GereChkbox('chkbx','0');">


0
isa-- Messages postés 77 Date d'inscription lundi 16 septembre 2013 Statut Membre Dernière intervention 11 mars 2020 1
23 nov. 2018 à 14:48
MAGNIFIQUE !
Ca fonctionne parfaitement !
Mille merci, Jordane, tu m'otes une belle épine du pied...
0