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

动态取得控件名?
有两一组控件,一组为Text1,Text2,Text3...的方本框,另一组为Select1,Select2,Select3...选择框.现当点击文本框时,取得相应的选择框.如点击Text1时取得Select1,类推?

------解决方案--------------------
document.getElementById( "Text1 ".replace(/Text/, "Select "))
------解决方案--------------------
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> new page </title>
<script>
function a(obj){
var objId=obj.id;
var index=objId.substring(4,5);
var selectValue=document.getElementById( "select "+index).value;
alert( "您选中的是text "+index+ ",select "+index+ "的值为 "+selectValue);
}
</script>
</head>
<body>
<input type=text id=text1 value= " " onclick= "a(this) ">
<input type=text id=text2 value= " " onclick= "a(this) ">
<input type=text id=text3 value= " " onclick= "a(this) ">
<select id=select1> <option value=1 selected> 1 </option> <option value=2> 2 </option> </select>
<select id=select2> <option value=3 selected> 3 </option> <option value=4> 4 </option> </select>
<select id=select3> <option value=5 selected> 5 </option> <option value=6> 6 </option> </select>
</body>
</html>