复选框全部选中代码:
<script type="text/javascript">
function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.Name != "chkAll"&&e.disabled!=true)
e.checked = form.chkAll.checked;
}
}
</script>
<input name="chkAll" type="checkbox" id="chkAll" οnclick=CheckAll(this.form) value="checkbox">选中/取消所有
判断是否选中任一复选框代码:
<script language="javascript">
function check()
{
len = document.form1.elements.length-1;
for(i=0,num=0;i<len;i++)
{
if(document.form1.elements[i].checked)
{num++;}
}
if(num>0)
return true;
else
{
alert("您未选中任何复选框");
return false;
}
}
</script>
<input name="submit" type="submit" onClick="return check()" value="确定">
<!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>判断复选框是否选中</title>
<scrīpt language="javascrīpt" type="text/javascrīpt">
function isSelect(){
var ōbj = document.getElementsByName('interest');
var len = obj.length;
var flag = false;
var display = '你的兴趣是:';
for(var i=0;i<len;i++){
if(obj[i].checked==true){
flag = true;
display += ' ' + obj[i].nextSibling.nodeValue;
}
}
if(flag)
alert(display);
else
alert('你没有选择任何兴趣');
}
</scrīpt>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<input type="checkbox" name="interest" value="1" />旅游
<input type="checkbox" name="interest" value="2" />购物
<input type="checkbox" name="interest" value="3" />逛街
<input type="checkbox" name="interest" value="4" />上网
<input type="checkbox" name="interest" value="5" />阅读
<input type="button" name="ok" value="确定" ōnclick="isSelect();" />
</form>
</body>
</html>