动态创建的元素可不可以通过name属性获取,如:document.getElementsByName("bbb")[0].value???
-----------------
以下是我的代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function add(){
 var oInput=document.createElement("input");
oInput.setAttribute("type","text");
oInput.setAttribute("value","1234567");
oInput.setAttribute("id","bbb1");
oInput.setAttribute("name","bbb");
var aaa=document.getElementById("aaa");
aaa.appendChild(oInput);}
function get(){
var ccc=document.getElementsByName("bbb");
//var ccc=document.getElementById("bbb1");
alert(ccc[0].value);}
</script>
</head><body>
<input type="button" onclick="get();" value="name取得值" /><br />
<input type="text" value="dddddddd333" name="ttt"/><br/>
<input type="button" onclick="add();" value="增加" /><br />
<div id="aaa">
</div></body>
</html>分析:如果通过动态元素的ID,是可以取得动态元素的value.

解决方案 »

  1.   


    <script type="text/javascript">
    function add(){
    document.getElementById("aaa").innerHTML = "<input type='text' name='bbb' value='123456' />";
    }
    function get(){
    alert(document.getElementsByName("bbb")[0].value);}
    </script>
      

  2.   


    function get(){
    /*var ccc=document.getElementsByName("bbb");
    //var ccc=document.getElementById("bbb1");
    alert(ccc[0].value);*/
    alert(document.getElementById("aaa").innerHTML)
    }
    原因.
      

  3.   

    Attributes can be included with the sTag as long as the entire string is valid HTML. You should do this if you wish to include the NAME attribute at run time on objects created with the createElement method.so you should do as below as you wanna Name attributenewInput = document.createElement("<INPUT TYPE='text' NAME='test' VALUE='123'>")
      

  4.   

    谢谢指点。但是,我想用这个创建元素
    document.createElement("input"); 这种方法是不是不能通过NAME属性获取对象。
      

  5.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function add(){
     var oInput=document.createElement("input");
    oInput.setAttribute("type","text");
    oInput.setAttribute("value","1234567");
    oInput.id="bbb1";
    oInput.name="bbb";
    var aaa=document.getElementById("aaa");
    aaa.appendChild(oInput);}
    function get(){
    var obj=document.getElementsByTagName("input")
    for (var i=0;i<obj.length;i++)
    if (obj[i].name=="bbb")
    alert(obj[i].value)
    }
    </script>
    </head><body>
    <input type="button" onclick="get();" value="name取得值" /><br />
    <input type="text" value="dddddddd333" name="ttt"/><br/>
    <input type="button" onclick="add();" value="增加" /><br />
    <div id="aaa">
    </div></body>
    </html>
      

  6.   

    我上面那段是出自HTML参考手册.
    说的是.要有name属性的话.不能用设置,要直接写在creatElement的参数中.caiying2009的方法也行.因为name变为自定义属性存在DOM对象了.不过还要封装一下.
      

  7.   

    caiying2009的方法
    不行。。
    oInput.id="bbb1";
    oInput.name="bbb";

    oInput.setAttribute("id","bbb1");
    oInput.setAttribute("name","bbb");
    效果是一样的
    都报0.value为空或不是对象
      

  8.   

    你看他取值...不是用getElementsByName的...
      

  9.   

    哦。。
    他那样比较复杂,
    要循环。
    因为我要创建10个左右input,text
    我 还是用你那种方法吧。第一种也行,非常感谢!!