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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="Author" content="Daniel Hagnoul">
<title>Forum jQuery</title>
<style>
/* Base */
div,p,h1,h2,h3,h4,h5,h6,ul,ol,dl,form,table,img {margin:0px; padding:0px; }
body {background-color:#dcdcdc; color:#000000; font-family:sans-serif; font-size:medium; font-style:normal; font-weight:normal; line-height:normal; letter-spacing:normal; }
h1,h2,h3,h4,h5 {font-family:serif; padding:6px; }
p, div, td {word-wrap:break-word; }
pre, code {white-space:pre-wrap; word-wrap:break-word; }
img, input, textarea, select {max-width:100%; }
img {border:none; }
h1 {font-size:2em; text-shadow: 4px 4px 4px #bbbbbb; text-align:center; }
p {padding:6px; }
ul,ol,dl {list-style:none; padding-left:6px; padding-top:6px; }
li {padding-bottom:6px; }
.conteneur {width:95%; min-width:800px; min-height:400px; margin:12px auto; background-color:#FFFFFF; color:#000000; border:1px solid #666666; }
/* article */
/*
.conteneur {border-collapse:separate; border-spacing:6px; }
.ligne {display:table-row; }
article {display:table-cell; text-align:center; vertical-align:middle; border:1px dotted grey; }
article:nth-of-type(1) {width:200px; height:200px; }
article:nth-of-type(2) {width:200px; height:200px; }
*/
/* -- */
.hide {display: none; }
</style>
</head>
<body>
<h1>Forum jQuery</h1>
<section class="conteneur">
<ul id="menu_selection">
<li>
<input type="checkbox" name="montre_tout_fils"/><label> : Montre les fils</label>
</li>
<li>
<input type="checkbox" name="coche_tout_fils_1"/><label> : Coche les fils 10</label>
<ul>
<li>
<input type="checkbox"/><label> : Coche fils 11</label><br/>
<input type="checkbox"/><label> : Coche fils 12</label>
</li>
</ul>
</li>
<li>
<input type="checkbox" name="coche_tout_fils_2"/><label> : Coche les fils 20</label>
<ul>
<li>
<input type="checkbox"/><label> : Coche fils 21</label><br/>
<input type="checkbox"/><label> : Coche fils 22</label>
</li>
</ul>
</li>
</ul>
</section>
<footer itemscope itemtype="http://data-vocabulary.org/Person">
<!-- Date ISO format long US : aaaa-mm-qqThh:mm:ss.nnn+hh:mm -->
<time datetime="2011-05-17T23:55:00.000+02:00" pubdate>2011-05-17</time> <span itemprop="name">Daniel Hagnoul</span> <a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
</footer>
<script charset="utf-8" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script>
$(function(){
// réinitialiser au chargement de la page
// prop() : http://api.jquery.com/prop/
$("input:checkbox").prop("checked", false);
$("#menu_selection > li > ul").addClass('hide');
$("input:checkbox[name='montre_tout_fils']").click(function(){
$("#menu_selection > li > ul").toggleClass('hide');
});
// version 1, comme votre code
/*
$("input:checkbox[name^='coche_tout_fils_']").click(function(){
$(this).parent().children("ul").children("li").children("input:checkbox").each(function (i, item) {
item.checked = !item.checked;
});
});
*/
/*
* Version 2
* On doit stocker la valeur du checkox père avant d'attribuer
* une valeur aux checkox contenu dans le parent, ce qui inclus
* le checkox père.
*
* name^= : http://api.jquery.com/?ns0=1&s=attribut&go=
*/
$("input:checkbox[name^='coche_tout_fils_']").click(function(){
var bool = this.checked;
$(this).parent().find("input:checkbox").prop("checked", bool);
});
});
</script>
</body>
</html> |
Partager