<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javaScript实现checkbox多项选择</title>
<script language="javaScript">
//全选或全部不选
function checkall(){
var isChecked=(document.ck.checkAll.checked == true);
var elements=document.ck.elements;
var counter=elements.length;
for(i=0;i<counter;i++){
var element=elements[i];
if(element.type=="checkbox"){
element.checked=isChecked;
}
}
}
//是否有checkbox被选中
function anyChecked(){
var elements=document.ck.elements;
var counter=elements.length;
for(i=0;i<counter;i++){
var element=elements[i];
if(element.checked == true){
return true;
}
}
return false;
}
</script>
</head>
<body>
<form name="ck" Method="post" action="a.asp" οnsubmit="return anyChecked()">
全选:<input type="checkbox" id="checkAll"οnclick="checkall()" />
多项选择框列表:
<input type="checkbox" name="cb" value="1"/>
<input type="checkbox" name="cb" value="2"/>
<input type="checkbox" name="cb" value="3"/>
<input name="Submit" type="submit" value="submit">
</form>
</body>
</html>