利用jQuery实现CheckBox全选/全不选/反选

时间:2020-12-13 19:45:21

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="jquery-1.8.2.min.js"></script>
<script>function selectall(item)
{
$('[type=checkbox]:checkbox').attr("checked", true);
}

function selectnothing()
{
alert(2);
$('[type=checkbox]:checkbox').attr('checked',false);
}

function reverse(item)
{
alert(3);
$('[name='+item+']:checkbox').each(function(){
this.checked=!this.checked;
})
}</script>
<body>
<input name="sports" type="checkbox" />游泳<br />
<input name="sports" type="checkbox" />看书<br />
<input name="sports" type="checkbox" />上网<br />
<input id="ls" name="sports" type="checkbox" />音乐<br />
<input name="sports" type="checkbox" checked="checked" />登山<br />
<input type="button" value="全选" onclick="selectall()" />
<input type="button" value="全不选" onclick="selectnothing()" />
<input type="button" value="反选" onclick="reverse('sports')" />
</body>