写在前面:在例子中直接使用attr( )时,会出现bug,第一次可以,后面就无效了,改成使用prop( ),完美解决。
使用的话:元素本身自带的属性用prop,自定义的用attr
全选,反选 源码
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<style>
table tr td{border:1px solid red;}
</style>
<body>
<table style="border:1px solid">
<tr><td><input type="checkbox" class="using"></td><td>阿sa</td></tr>
<tr><td><input type="checkbox" class="using"></td><td>李沁</td></tr>
<tr><td><input type="checkbox" class="using"></td><td>热巴</td></tr>
<tr><td><input type="checkbox" id="allchoose">全选</td></tr>
</table>
<script>
$(function(){
$('#allchoose').change(function(){
if($('#allchoose').is(':checked')){
$('.using').prop('checked',true)
}else{
$('.using').prop('checked',false)
}
})
})
</script>
</body>
</html>