<HTML>
 <HEAD>
<script>
function set(){
var doms=document.getElementById('dd');

var ipt=document.createElement("input");
ipt.type="text";
ipt.maxlength=50;
ipt.name = "ip";
ipt.value = "111";
doms.appendChild(ipt);
var v = document.getElementsByName("ip")[0].value;
alert(v);
}
</script>
 </HEAD> <BODY>
  <div id = "dd"></div>
  <input type = button value = "set" onclick = "set();"/>
 </BODY>
</HTML>结果说没定义。
但是如果用ipt.id,然后后面用ById的话,就可以找到。是什么原因呢?

解决方案 »

  1.   

    不知道你代码发全没。。
    var v = document.getElementsByName("ip")[0].value; 
    并没有name为ip的代码
      

  2.   

    ipt.name = "ip"; 没有看到么?
      

  3.   

    IE下那样设置name不起作用的.FF是可以的.要说为什么,问MS去吧....
    可以这样写try{
    ipt=document.createElement("<input name='ip' />"); // IE
    }catch(e){
    ipt=document.createElement("input");               // FF
    ipt.name="ip";
    }
      

  4.   

    额 漏了声明 在try之前加上var ipt = null; ...
      

  5.   

     Name属性必须特殊对待,只要如此创建就能找到了。   
        
      MSDN原文如此:   
      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.
    创建元素的时候可以,在创建的时候指定名字。function set(){  var doms=document.getElementById('dd');  var ipt = document.createElement("<input name='ip' />"); 
    doms.appendChild(ipt); 
    ipt.type="text"; 
    ipt.maxlength=50; 
    ipt.value = "111"; 

    var v = document.getElementsByName("ip")[0].value; 
    alert(v); 
      

  6.   


    <HTML> 
    <HEAD> 
    <script> 
    function set(){ 
    var doms=document.getElementById('dd'); var ipt=document.createElement("input"); 
    ipt.type="text"; 
    ipt.maxlength=50; 
    ipt.name = "ip"; 
    ipt.value = "111"; 
    doms.appendChild(ipt); 
    var v = document.getElementsByName("ip")[0].value; 
    alert(v); 

    </script> 
    </HEAD> <BODY> 
      <div id = "dd"> </div> 
      <input type = button value = "set" onclick = "set();" name="ip"/> 
    </BODY> 
    </HTML> 
    我这样改后点击set 弹出个对话框 内容是set 并且得到一个111
      

  7.   

    IE下input.name='ip'并未赋值上!
      

  8.   

    name在IE中只能这样创建
    var ipt=document.createElement("<input name='ip'>"); 
      

  9.   


    <HTML> 
    <HEAD> 
    <script> 
    function set(){ 
    var doms=document.getElementById('dd'); var ipt=document.createElement("<input name='ip' />"); 
    ipt.type="text"; 
    ipt.maxlength=50; 
    ipt.value = "111"; 
    doms.appendChild(ipt); 
    var v = document.getElementsByName('ip')[0].value; 
    alert(v); } 
    </script> 
    </HEAD> <BODY> 
      <div id = "dd"> </div> 
      <input type = button value = "set" onclick = "set();"/> 
    </BODY> 
    </HTML>
      

  10.   

    因为IE这个“特殊分子”的存在,造成好多js并不遵循W3C的规范。IE里面规定,所有标签的name属性一旦制定,是无法更改的。而FireFox和其他一些浏览器是允许更改name属性的。所有,必须用document.createElement('<input type="checkbox" name="hello">')来指定name属性。
    ---------- 给分 --------------
      

  11.   

    但是document.createElement(' <input type="checkbox" name="hello">')这样一来的话,
    火狐下不支持啊.
      

  12.   

    所以四楼的才用try的啊,只有name不能这样设置,type就按你那样设置就行了
      

  13.   

    The NAME attribute cannot be set at run time on elements dynamically created with the createElement method. To create an element with a name attribute, include the attribute and value when using the createElement method.ExamplesThe following example shows how to set the NAME attribute on a dynamically created A element.var oAnchor = document.createElement("<A NAME='AnchorName'></A>");