①Default.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function checkAll() {
var a = document.getElementsByName("ck");
for (var i = 0; i < a.length; i++) {
var c = a[i];
c.checked = true;
}
}
function checkNOAll() {
var a = document.getElementsByName("ck");
for (var i = 0; i < a.length; i++) {
a[i].checked = false;
}
}
function checkReverse() {
var a = document.getElementsByName("ck");
for (var i = 0; i < a.length; i++) {
var c = a[i];
if (c.checked == true) {
c.checked = false;
} else {
c.checked = true;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Checkbox1" type="checkbox" name="ck"/>路飞
<input id="Checkbox2" type="checkbox" name="ck"/>索隆
<input id="Checkbox3" type="checkbox" name="ck"/>山治
<input id="Checkbox4" type="checkbox" name="ck"/>萨博<br />
<input id="Button1" type="button" value="全选" οnclick="checkAll()" />
<input id="Button2" type="button" value="全不选" οnclick="checkNOAll()"/>
<input id="Button3" type="button" value="反选" οnclick="checkReverse()"/>
</div>
</form>
</body>
</html>
②效果图
Ⅰ.全选
Ⅱ.全不选
Ⅲ.反选
③说明:
Ⅰ.选中“路飞”“索隆”点击全选则全部选中。
Ⅱ.选中“路飞”“索隆”“萨博”点击全不选则全部取消选中。
Ⅲ.选中“山治”“萨博”点击反选则选择其他两个选项(“路飞”“索隆”)。