一、jquery 判断checkbox是否选中
1、方法1:
if($('#checkOne').get(0).checked){
//执行选中后代码
}
2、方法2:个人常用
if($('#checkOne').is(':checked')){
//执行选中后代码
}
3、方法3:
if($('#checkOne').attr('checked')){
//执行选中后代码
}
4、方法4:jQuery1.6及以上版本有效
if($('#checkOne').prop('checked')){
//执行选中后代码
}
二、checkbox全选
HTML页面
<table>
<thead>
<tr>
<th width="8"><input type="checkbox" class="checkAll">全选</th>
<th>ID</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="checkOne"></td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" class="checkOne"></td>
<td>2</td>
</tr>
<tr>
<td><input type="checkbox" class="checkOne"></td>
<td>3</td>
</tr>
<tr>
<td><input type="checkbox" class="checkOne"></td>
<td>4</td>
</tr>
<tr>
<td><input type="checkbox" class="checkOne"></td>
<td>5</td>
</tr>
</tbody>
</table>
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
//全选
$('.checkAll').on('click',function(){
var c = $(this).prop('checked');
$('.checkAll').prop('checked',c);
$('.checkOne').prop('checked',c);
})
//单选
$('.checkOne').on('click',function(){
var all_num = parseInt($('.checkOne').length); //记录总数
var checked_num = parseInt($('.checkOne:checked').length); //选中的数量
//记录总数与选中数量相等时全选按钮选中,否则全选按钮取消选中
if(all_num==checked_num){
$('.checkAll').prop('checked',true);
}else{
$('.checkAll').prop('checked',false);
}
})
})
</script>