js中验证checkbox的问题

来源:百度知道 编辑:UC知道 时间:2024/06/09 18:23:04
<html>
<head>
<title>Checkbox检查器</title>
<script type="text/javascript">
function check_box() {
var count=document.myckeckbox.checkThis.length;
var j=0;
for(var i=0;i<count;i++)
{
if (document.myform.checkThis[i].checked)
++j;
}
if(j==0)
{alert("至少选择一个!!");
return false;}
else
{ return true;}

</script>
</head>
<body>
<form name="myform" action="" onsubmit="return check_box();">
<input type="checkbox" name="checkThis[]" value="1">1<br>
<input type="checkbox" name="checkThis[]" value="2">2<br>
<input type="checkbox" name="checkThis[]" value="3">3<br>
<input typ

下面是修改后的代码:
修改的错误:
1、首先把你的checkbox的 name名字改一下,不能以[],结尾,否则报错
2、var count=document.myckeckbox.checkThis.length; 改为

var count=document.myform.checkThis.length;
3、脚本的最后 少一个右大括号 "}"

<html>
<head>
<title>Checkbox检查器</title>
<script type="text/javascript">
function check_box() {
var count=document.myform.checkThis.length;
alert(count);
var j=0;
for(var i=0;i<count;i++)
{
if (document.myform.checkThis[i].checked)
++j;
}
if(j==0)
{alert("至少选择一个!!");
return false;}
else
{ return true;}

}

</script>
</head>
<body>
<form name="myform" action="" onsubmit="return check_box();">
<input type="checkbox" name="checkThis" value="1">1<br>
<input ty