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

js 能取到所有相同ID的值吗,该怎么处理

2012-03-12 
js 能取到所有相同ID的值吗formname form1 inputid 1 name ch1 type checkbox value1in

js 能取到所有相同ID的值吗
<form       name= "form1 ">      
    <input       id= "1 "       name= "ch1 "       type= "checkbox "       value=1>      
    <input       id= "1 "       name= "ch2 "       type= "checkbox "       value=2>      
    <input       id= "1 "       name= "ch3 "       type= "checkbox "       value=3>      
    <input       id= "2 "       name= "ch1 "       type= "checkbox "       value=1>      
    <input       id= "2 "       name= "ch2 "       type= "checkbox "       value=2>      
    <input       id= "2 "       name= "ch3 "       type= "checkbox "       value=3>    
    <input       type= "button "       onClick= "check() ">      
    </form>       能通过ID获取六个的值吗??

[解决办法]
id理论上讲应该是唯一的,只是IE里混用


var objs=document.getElementsByTagName( "input ")
for(var i=0;i <objs.length;i++)
{
if((objs[i].text= "checkbox ")&&(/^ch/.test(objs[i].name)))
{
alert(objs[i].value)
}
}
[解决办法]
<form name= "form1 ">
<input id= "1 " name= "ch1 " type= "checkbox " value=1>
<input id= "1 " name= "ch2 " type= "checkbox " value=2>
<input id= "1 " name= "ch3 " type= "checkbox " value=3>
<input id= "2 " name= "ch1 " type= "checkbox " value=1>
<input id= "2 " name= "ch2 " type= "checkbox " value=2>
<input id= "2 " name= "ch3 " type= "checkbox " value=3>
<input type= "button " onClick= "check() ">
</form>
<script language=javascript>
var objs=document.getElementsByTagName( "input ")
for(var i=0;i <objs.length;i++)
{
if((objs[i].text= "checkbox ")&&(/^ch/.test(objs[i].name)))
{
alert(objs[i].value)
}
}

</script>

热点排行