读书人

怎么用循环给表单内每个checkbox加个事

发布时间: 2012-04-04 16:38:51 作者: rapoo

如何用循环给表单内每个checkbox加个事件
<html>

<head>

<title> </title>

</head>

<body>
<span class= "STYLE1 "> 欢迎订阅时事通讯: </span> <br />
<br />
请选择您要订阅的时事通讯: <br />
<form id= "form1 " name= "form1 " method= "post " action= " ">
<label>
<input type= "checkbox " name= "C " value= "C " />
C </label>
<br />
<label>
<input type= "checkbox " name= "C++ " value= "C++ " />
C++ </label>
<br />
<label>
<input type= "checkbox " name= "C# " value= "C# " />
C# </label>
<br />
<label>
<input type= "checkbox " name= "JAVA " value= "JAVA " />
JAVA </label>
<br />
<label>
<input type= "checkbox " name= "VB " value= "Visual Basic " />
Visual Basic </label>
<br />
<label>
<input type= "checkbox " name= "ORC " value= "Oracle " />
Oracle </label>
<br />
<label>
<input type= "checkbox " name= "SQL " value= "Sql Server " />
SQL Server </label>
<br />
<label>
<input type= "text " name= "textfield " />
</label>
<br />
<br />

</form>
<script language= "javascript " type= "text/javascript ">

for (var i=0;form1.checkbox.length;i++) { //这里取表单中checkbox个数应该有问题
if (checkbox[i].type= "checkbox "){
checkbox[i].onclick=function() {
if(this.checked==true){
form1.textfield.value= "你选中了 "+form1.checbox[i].value+ "语言选项。 ";
} else {
form1.textfield.value= "你取消了 "+form1.checbox[i].value+ "语言选项。 ";
}
}
}

}
</script>

</body>

</html>

------解决方案--------------------


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " ">

</HEAD>

<BODY>
<input type= 'checkbox ' name= 'C2 ' value = '0 '>
<input type= 'checkbox ' name= 'C2 ' value = '1 '>
<input type= 'checkbox ' name= 'C2 ' value = '2 '>
</BODY>
<script language= "javascript ">
alert( " ")
function gettd()
{
alert( "1 ")
var a=document.getElementsByTagName( "input ");
alert(a.length)
for(var i=0;i <a.length;i++)
{
a[i].onclick=(function (i){return function(){updownmouse(i);}})(i)
}
}
function updownmouse(i)
{
alert(i);//这里输出的i总为500;
}
gettd();
</script>
</HTML>
[解决办法]
if (a[i].type== "checkbox ")
符号写错了

读书人网 >JavaScript

热点推荐