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

document.getElementsByName问题
<html> <head> <script>
function   getValue()
{
var   tt=document.getElementsByName( "pro_class1 ").value;  
alert   (tt);
}
</script> </head>
<body>
<form   name= "form1 ">
          <select   name= "select1 "   onChange= "getValue() ">
                    <option   value= "1 "> 1 </option>
                    <option   value= "2 "> 2 </option>
          </select>
</form>
</body> </html>

================================
正常的话,不是应该弹出一个包含我选择了值的对话框吗?
但是我这里弹出了undefine.
怎么回事呢???是不是我哪里写错了,我眼掘,大家帮忙看看好吗

------解决方案--------------------
<html> <head> <script>
function getValue()
{
var tt=document.getElementsByName( "select1 ");
alert (tt[0].getAttribute( "value "));
}
</script> </head>
<body>
<form name= "form1 ">
<select name= "select1 " onChange= "getValue() ">
<option value= "1 "> 1 </option>
<option value= "2 "> 2 </option>
</select>
</form>
</body> </html>
------解决方案--------------------
L@_@K

<html> <head> <script>
function getValue()
{
var tt=document.getElementsByName( "select1 ");
alert( "length: " + tt.length);
alert( "value: " + tt[0].value);
}
</script> </head>
<body>
<form name= "form1 ">
<select name= "select1 " onChange= "getValue() ">
<option value= "1 "> 1 </option>
<option value= "2 "> 2 </option>
</select>
</form>
</body> </html>
------解决方案--------------------
<html> <head> <script>
function getValue()
{
var tt=document.getElementById( "select1 ").value;
alert (tt);
}
</script> </head>
<body>
<form name= "form1 ">
<select name= "select1 " onChange= "getValue() ">
<option value= "1 "> 1 </option>
<option value= "2 "> 2 </option>
</select>
</form>
</body> </html>

------解决方案--------------------
1、“pro_class1”这个名称没有
2、“getElementsByName”得到的是个数组,name允许重名。
<html> <head> <script>
function getValue()
{
var tt=document.getElementsByName( "select1 ")[0].value;
alert (tt);
}
</script> </head>
<body>
<form name= "form1 ">
<select name= "select1 " onChange= "getValue() ">
<option value= "1 "> 1 </option>
<option value= "2 "> 2 </option>