今天在页面中使用jQuery实现了全选框操作:
1、设计一个简单的表格:
设置表格列标题,在列标题中的单选框为全选框;
设置表格题,表格题的单选框就是普通的单选框啦;
<table>
<thead>
<tr>
<td>
<input type="checkbox" onclick="selectAll(this.checked)" />
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="check" />
</td>
<td>
<input type="checkbox" name="check" />
</td>
<td>
<input type="checkbox" name="check" />
</td>
</tr>
</tbody>
</table>
2、通过jQuery实现当点击全选框的时候所有单选框都选中,当再次点击全选框的时候所有单选框都是未选中状态
function selectAll(selectStatus){//传入参数(全选框的选中状态)
//根据name属性获取到单选框的input,使用each方法循环设置所有单选框的选中状态
if(selectStatus){
$("input[name='check']").each(function(i,n){
n.checked = true;
});
}else{
$("input[name='check']").each(function(i,n){
n.checked = false;
});
}
}