input 标签 的名字问题

本文探讨了在JavaScript中为何无法通过动态方式更改input元素的name属性,并提供了具体实例及MSDN官方解释。同时,文章还分享了如何正确地创建带有name属性的动态元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       起因:
       开始的时候是,群组中有人问到为什么改变了一个input 标签的名字,用outterHTML()无法把改变后的名字显示出来,而显示的仍旧是以前的那个名字,于是我也写了一个函数,果然改变名字无法显示处改变后的新name. 当时觉得奇怪,随便改了一些其它的属性都没有问题,就是说都可以改变,包括id,value,size等等.
        我开始怀疑是不是用错了赋值方法.
       原先的大体步骤如下.
       [code]
              <form name="form1">
                       <input type="text" id="data" size="10" name="old" value=""><br />
                       <input type="button" value="改变值" onclick="change()"><br>
                       <input type="button" value="新ID下填充" onclick="fill()"><br/>
                       <input type="button" value="新name下填充" onclick="fill1()"><br/>
             </form>

<script type="text/javascript">
<!--
         var changeFlag = false;

         //改变input的名字(name)和ID
         function change(){
            if(changeFlag == true){
               return; //已操作过了
            }
               var myNode = document.getElementById("data");
               var newNode = myNode.cloneNode();//拷贝原节点,包括name属性
               newNode.name="new";                     //变化名称
                newNode.id  = "newNode";               //变化ID
                newNode.onclick=changeP;             
                myNode.parentNode.replaceChild(newNode,myNode); //替代原节点
                changeFlag = true;  
          }
          function changeP(){
                alert(event.srcElement.name) ;//显示出name的值已改变
          }
       function fill(){
          if(changeFlag){
                  //document.form1.cc.value="yes"; //无效
                 document.getElementById("newNode").value="新的name:"
                                               + document.getElementById("newNode").name; //可以访问
           }else{
             alert(" 你还没有按照测试要求改变名称值");
           }
 }

  function fill1(){
          if(changeFlag){                
                     document.form1.cc.value ="新的name:" + document.form1.cc.name;                    
           }else{
             alert("你还没有按照测试要求改变名称值");
           }


  }
// -->
</script>

以上可以充分的看到这种现象,当然不能说是问题,如果非要说问题,也是自己的认识不够的问题. 那么为什么会出现这样的情况呢?
        开始赋值用的是 nodeName.name = "newName",没定位到input,那就试过用 nodeName.setAttribute("name","newName"), 情况依旧, 我试着改变了size,效果很明显,的确长了很多,以及其它的属性也都试了,都没有问题,很是纳闷.
        好在蓝点版主是个热心人.给出了一段msdn说明:

Remarks
When submitting a form, use the name property to bind the value of the control. The name is not the value displayed for the input type=button, input type=reset, and input type=submit input types. The internally stored value, not the displayed value, is the one submitted with the form.

Microsoft JScript allows the name to be changed at run time. This does not cause the name in the programming model to change in the collection of elements, but it does change the name used for submitting elements.

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.

Examples

The following example shows how to set the NAME attribute on a dynamically created A element.
var oAnchor = document.createElement("<A NAME='AnchorName'></A>");

其中cannot be set at run time on elements dynamically  ,就是原因所在,不过不知道为什么要这样设计,或是自己没有考虑到那些细节.
不得而知.望路人点拨.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值