jquery checkbox全選/全不選を実現する汎用書き方
3108 ワード
<br> //name html chechbox , id( )
<br> function getValues() {
<br> var aV = getCheckboxValues("a");
<br> var bV = getCheckboxValues("b");
<br> var cV = getCheckboxValues("c");
<p></p>
<p> var result = "a : " + (aV=="" ? " " : aV) + "
" +<br> "b : " + (bV == "" ? " " : bV) + "
" +<br> "c : " + (cV == "" ? " " : cV);</p>
<p> alert(result);<br> }</p>
<p> function getCheckboxValues(name) {<br> return $(":checkbox[name="+name+"]:checked").map(function(){ <br> return $(this).val(); <br> }).get().join(",");<br> }</p>
<p> function chkAll(obj) {<br> var name = $(obj).attr("name");<br> // , jquery , <br> //1. jquery 1.6 <br> //$(":checkbox[name=" + name.substring(0, 1) + "]").prop("checked", $(obj).prop("checked"));<br> //2. jquery 1.6 <br> //$(":checkbox[name=" + name.substring(0, 1) + "]").attr("checked", $(obj).attr("checked"));<br> //3. <br> $(":checkbox[name=" + name.substring(0, 1) + "]").each(function () {<br> this.checked = obj.checked;<br> });<br> }<br>
1 (a): /
a1
a2
a3
a4
2 (b): /
b1
b2
b3
b4
3 (c): /
c1
c2
c3
c4