首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2012-04-04 
如何用循环给表单内每个checkbox加个事件htmlheadtitle/title/headbodyspanclass STYLE1

如何用循环给表单内每个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 ")
符号写错了

热点排行