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

关于getElementsByName,很奇怪的问题。
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN "   "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> 企业审核/已审核 </title>
<script   language= "JavaScript "   src= "../../scripts/action.js ">
</script>
<script   language= "javascript "   type= "text/javascript ">
function   ccc()
{
      if(document.getElementsByName( "bbb ").value   !=   "undefined ")
      {
            alert(document.getElementsByName( "bbb ").value);
      }
}
</script>
</head>
<body   bgcolor= "#FFFFFF "   marginheight=0   marginwidth=0   leftmargin=0>
    <Form   name= "form1 "   method= "POST "   action= " ">
        <input   type= "text "   name= "bbb "   value= "111 "> </input>
        <input   type= "text "   name= "bbb "   value= "222 "> </input>
        <input   type= "button "   onClick= "ccc(); ">
    </form>
</body>
</html>

运行这段代码,看看出现的问题,感觉很奇怪。

------解决方案--------------------
getElementsByName获取的是数组,你应该这样用:

document.getElementsByName( "bbb ")[0].value
------解决方案--------------------
alert(document.getElementsByName( "bbb ").value);

document.getElementsByName( "bbb ")得到的是 [object]


alert(document.getElementsByName( "bbb ")[0].value);这样可以取到第一个的value

另外,undefined跟 "undefined "是不一样的! 未初始化 跟 字符串 是不会相等的~