今天在页面中使用jQuery实现了全选框操作,如图:
具体的实现过程很简单:
第一步
设计一个简单的表格:
设置表格列标题,在列标题中的单选框为全选框;
设置表格题,表格题的单选框就是普通的单选框啦:
<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>
第二步
通过jQuery实现当点击全选框的时候所有单选框都选中,当再次点击全选框的时候所有单选框都是未选中状态:
function selectAll(selectStatus){//传入参数(全选框的选中状态)
//根据name属性获取到单选框的input,使用each方法循环设置所有单选框的选中状态
$("input[name='check']").each(function(i,n){
n.checked = status;
});
}
好啦,大功告成!
ps:亲们,不要忘了引入jQuery的js文件!!!