以前总是自己写纯JS.现在既然知道怎么写了.那如何用JQ写得更简洁呢.这样也能学到新的东西.如果乎百度一下果然发现了好东东.感谢OSC的iuhoay.
代码如下:
<
script
type
="text/javascript"
src
="/web/bzz_index/password/js/jquery-1.7.1.min.js"
></
script
>
< script language ="JavaScript" >
$( function () {
$( " #ckAll " ).click( function () {
$( " input[name='sub'] " ).prop( " checked " , this .checked);
});
$( " input[name='sub'] " ).click( function () {
var $subs = $( " input[name='sub'] " );
$( " #ckAll " ).prop( " checked " , $subs.length == $subs.filter( " :checked " ).length ? true : false );
});
});
</ script >
< script language ="JavaScript" >
$( function () {
$( " #ckAll " ).click( function () {
$( " input[name='sub'] " ).prop( " checked " , this .checked);
});
$( " input[name='sub'] " ).click( function () {
var $subs = $( " input[name='sub'] " );
$( " #ckAll " ).prop( " checked " , $subs.length == $subs.filter( " :checked " ).length ? true : false );
});
});
</ script >
<
input
type
="checkbox"
id
="ckAll"
/>check all
<
br
/>
< input type ="checkbox" name ="sub" />1 < br />
< input type ="checkbox" name ="sub" />2 < br />
< input type ="checkbox" name ="sub" />3 < br />
< input type ="checkbox" name ="sub" />4 < br />
< input type ="checkbox" name ="sub" />1 < br />
< input type ="checkbox" name ="sub" />2 < br />
< input type ="checkbox" name ="sub" />3 < br />
< input type ="checkbox" name ="sub" />4 < br />