首页 > 代码库 > js 实现 复选框全选与全不选
js 实现 复选框全选与全不选
<html>
<head>
<script>
function selectAll(){
var checklist = document.getElementsByName ("selected");
if(document.getElementById("controlAll").checked)
{
for(var i=0;i<checklist.length;i++)
{
checklist[i].checked = 1;
}
}else{
for(var j=0;j<checklist.length;j++)
{
checklist[j].checked = 0;
}
}
}
</script>
</head>
<body>
<form>
<input on
click="selectAll()" type="checkbox" name="controlAll" style="controlAll" id="controlAll"/>全选<br>
1:<input type="checkbox" name="selected" value="http://www.mamicode.com/1"/><br>
2:<input type="checkbox" name="selected" value="http://www.mamicode.com/2"/><br>
3:<input type="checkbox" name="selected" value="http://www.mamicode.com/3"/><br>
4:<input type="checkbox" name="selected" value="http://www.mamicode.com/4"/><br>
5:<input type="checkbox" name="selected" value="http://www.mamicode.com/5"/><br>
6:<input type="checkbox" name="selected" value="http://www.mamicode.com/6"/><br>
</form>
<p>
或
<script type="text/javascript">
function select_all(id){
var checklist=$("input[name=‘"+id+"a‘]");//全选对象
var ck=document.getElementById(id);//单击全选中,再点全取消
if(ck.checked){
for(var i=0;i<checklist.length;i++){
checklist[i].checked = 1;
}
}else{
checklist.each(
function(){
checklist.attr("checked",false);
}
);
}
}
</script>
js 实现 复选框全选与全不选