Id在页面上唯一的,用name来标记<!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></head>
<body>
<input type="checkbox" name="chk" value="1" />1<br />
<input type="checkbox" name="chk" value="2" />2<br />
<input type="checkbox" name="chk" value="3" />3<br />
<input type="checkbox" name="chk" value="4" />4<br />
<input type="checkbox" name="chk" value="5" />5<br />
<input type="button" id="btn" value="click" onclick="btnClick();" />
<script type="text/javascript">
function btnClick()
{
var chkDom=document.getElementsByName("chk");
for(var i=0;i<chkDom.length;i++)
{
if(chkDom[i].checked)
{
alert(chkDom[i].value + "is checked");
}
}
}
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title></head>
<body>
<input type="checkbox" name="chk" value="1" />1<br />
<input type="checkbox" name="chk" value="2" />2<br />
<input type="checkbox" name="chk" value="3" />3<br />
<input type="checkbox" name="chk" value="4" />4<br />
<input type="checkbox" name="chk" value="5" />5<br />
<input type="button" id="btn" value="click" onclick="btnClick();" />
<script type="text/javascript">
function btnClick()
{
var chkDom=document.getElementsByName("chk");
for(var i=0;i<chkDom.length;i++)
{
if(chkDom[i].checked)
{
alert(chkDom[i].value + "is checked");
}
}
}
</script>
</body>
</html>
var f = document.getElementById('main1[]')id不该相同。
如果要相同,。
getElementsByTagName('input')if
elem.id=='main1[]'&&elem.type=='checkbox'&&elem.checked==true
<head>
<script type="text/javascript" src='jquery-1.4.2.min.js'></script>
</head>
<body> <script>
function check() {
var f = document.getElementsByTagName('input'); for (var i=0;i<f.length;i++) {
var e = f[i];
if (e.type == "checkbox" && e.checked) return true;
}
alert("至少选一项");
return false;
}
</script>
<form id="form" name="form" method="post" onsubmit="return check();" action="">
<input type="checkbox" name="add[]" id="main1" value="1"/>
.
.
.
<input type="checkbox" name="add[]" id="main2" value="6"/>
<input type="checkbox" name="add[]" id="add1" value="7"/>
.
.
.
<input type="checkbox" name="add[]" id="add2" value="10"/>
<input type="submit" value="提交" />
</form>
</body>
</html>
<!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>
</head>
<body>
<input type="checkbox" id="chk0" name="chk" value="1" />1<br />
<input type="checkbox" id="chk1" name="chk" value="2" />2<br />
<input type="checkbox" id="chk2" name="chk" value="3" />3<br />
<input type="checkbox" id="chk3" name="chk" value="4" />4<br />
<input type="checkbox" id="chk4" name="chk" value="5" />5<br />
<input type="button" id="btn" value="click" onclick="btnClick();" />
<script type="text/javascript">
function btnClick()
{
for(var i=0;i<5;i++)
{
var chk=document.getElementById("chk"+i);
if(chk.checked)
{
alert(chk.value + "is checked");
}
}
}
</script>
</body>
</html>
<br />
<input type="checkbox" id="main1[]" name="chk" value="2" />2
<br />
<input type="checkbox" id="main1[]" name="chk" value="3" />3
<br />
<input type="checkbox" id="chk3" name="chk" value="4" />4
<br />
<input type="checkbox" id="chk4" name="chk" value="5" />5
<br />
<input type="button" id="btn" value="click" onclick="btnClick();" />
<script type="text/javascript">
var $input = document.getElementsByTagName('input');
var $main1s = (function($input) {
var i = 0, result = [];
for (; i < $input.length; i++) {
if ($input[i].id === 'main1[]') {
result.push($input[i]);
}
}
return result;
}($input)); function btnClick() {
var i=0,flag=true;
for(;i<$main1s.length;i++){
if($main1s[i].checked===true){
flag=false;
break;
}
}
if(flag){
alert('must be one');
}
}
</script>