给你个例子:
<checkbox name="checkbox" value="1" > 选项1</checkbox>
<checkbox name="checkbox" value="2" > 选项2</checkbox>
<br>
<br>
<checkbox name="checkbox" value="3" > 选项3</checkbox>
<br>
<br>
<checkbox name="checkbox" value="4" > 选项4</checkbox>
<br>
<br>
<checkbox name="checkbox" value="5" >选项5</checkbox>
<br>
<br>
<checkbox name="selectFlag" value="5" onclick="checkAll()">全选</checkbox>
<checkbox value="5" onclick="checkOther()">反选</checkbox>
<script language="javascript">
//全选
function checkAll()
{
var object = document.getElementsByName("checkbox");
var length = object.length;
var checked = document.getElementById("selectFlag").checked;
for (var i = 0; i < length; i++)
{
object[i].checked=checked;
}
}
//反选
function checkOther()
{
var object = document.getElementsByName("checkbox");
var length = object.length;
for (var i = 0; i < length; i++)
{
if(object[i].checked)
{
object[i].checked=false;
}
else
object[i].checked="true";
}
}
</script>
<checkbox name="checkbox" value="1" > 选项1</checkbox>
<checkbox name="checkbox" value="2" > 选项2</checkbox>
<br>
<br>
<checkbox name="checkbox" value="3" > 选项3</checkbox>
<br>
<br>
<checkbox name="checkbox" value="4" > 选项4</checkbox>
<br>
<br>
<checkbox name="checkbox" value="5" >选项5</checkbox>
<br>
<br>
<checkbox name="selectFlag" value="5" onclick="checkAll()">全选</checkbox>
<checkbox value="5" onclick="checkOther()">反选</checkbox>
<script language="javascript">
//全选
function checkAll()
{
var object = document.getElementsByName("checkbox");
var length = object.length;
var checked = document.getElementById("selectFlag").checked;
for (var i = 0; i < length; i++)
{
object[i].checked=checked;
}
}
//反选
function checkOther()
{
var object = document.getElementsByName("checkbox");
var length = object.length;
for (var i = 0; i < length; i++)
{
if(object[i].checked)
{
object[i].checked=false;
}
else
object[i].checked="true";
}
}
</script>