日期:2014-05-16  浏览次数:20309 次

如何根据文本框id固定的字符串得到对象?
<INPUT   id= "text1 "   TYPE= "text "   NAME=txt>
<INPUT   id= "text2 "   TYPE= "text "   NAME=txt1>
<INPUT   id= "text3 "   TYPE= "text "   NAME=txt2>
<INPUT   id= "text4 "   TYPE= "text "   NAME=txt3>
<INPUT   id= "text5 "   TYPE= "text "   NAME=txt4>


已知文本框的id前面固定的字符是text,但后面的数字是动态的,并不知道是多少,
请问有办法根据id得到文本框对象的值吗?

------解决方案--------------------
行不?

L@_@K

<body>
<INPUT id= "text1 " TYPE= "text " NAME=txt value= "1 ">
<INPUT id= "text2 " TYPE= "text " NAME=txt1 value= "11 ">
<INPUT id= "text3 " TYPE= "text " NAME=txt2 value= "111 ">
<INPUT id= "test1 " TYPE= "text " NAME=txt4 value= "22 ">
<INPUT id= "text4 " TYPE= "text " NAME=txt3 value= "1111 ">
<INPUT id= "text5 " TYPE= "text " NAME=txt4 value= "11111 ">
<INPUT id= "test0 " TYPE= "text " NAME=txt4 value= "2 ">
<script type= "text/javascript ">
<!--
var colInput = document.getElementsByTagName( "input ");
alert( "Count: " + colInput.length);
for (var i=0; i <colInput.length; i++)
{
if (colInput[i].type== "text "
&& colInput[i].id.indexOf( "text ")==0)
{
alert(colInput[i].value);
}
}

//-->
</script>
</body>
------解决方案--------------------
<INPUT id= "text1 " TYPE= "text " NAME=txt>
<INPUT id= "text2 " TYPE= "text " NAME=txt1>
<INPUT id= "text3 " TYPE= "text " NAME=txt2>
<INPUT id= "text4 " TYPE= "text " NAME=txt3>
<INPUT id= "text5 " TYPE= "text " NAME=txt4>
<script>
for(var i=0;i <5;i++)
{
var obj = eval( "document.getElementById( 'text " + i + " ') ");
}
</script>
------解决方案--------------------
<script type= "text/javascript ">
function getTextObjById(id)
{
return document.getElementById( "text "+id).value;
}
</script>
------解决方案--------------------
<script language= "javascript ">
function testGG(){
var xxx = document.getElementsByTagName( "input ");
var re = /^text(\d*)$/
for(var i = 0;i < xxx.length;i++){
if(re.test(xxx[i].id)){
alert(xxx[i].id);
}

}

}
</script>

<INPUT id= "text1 " TYPE= "text " NAME=txt>
<INPUT id= "text2 " TYPE= "text " NAME=txt1>
<INPUT id= "text3 " TYPE= "text " NAME=txt2>
<INPUT id= "text4 " TYPE= "text " NAME=txt3>
<INPUT id= "text5 " TYPE= "text " NAME=txt4>
<input id= "xxx " type= "text " name=txt5>
<input type= "button " value= "ok " onClick= "tes