代码之家  ›  专栏  ›  技术社区  ›  jantimon

jquery克隆问题

  •  6
  • jantimon  · 技术社区  · 16 年前

    我试图克隆一个DIV并更改这个DIV中输入字段的名称。 它对大多数浏览器都很有用,但IE7不会更改输入字段的名称属性。

    演示: http://jsbin.com/iduro/7

    HTML

    <body>
      <pre></pre>
      <div><input value="Hello World" name="test"></div>
    </body>
    

    JS

    var lastRow = $("body div:last"),
        newRow  = lastRow.clone(true)
                  .show()
                  .insertAfter(lastRow);
    
    newRow.find('input').attr("name","test2");
    
    $("pre").text( newRow[0].innerHTML );
    

    结果:

    火狐:(工作) <input value="Hello World" name="test2">

    IE8(工程) <INPUT value="Hello World" name=test2 jQuery1273063250500="4">

    IE7(错误): <INPUT value="Hello World" name=test jQuery1273063303968="4">

    如您所见,IE7的名称不会更改为test2。

    是否有明显的原因或工作?

    4 回复  |  直到 15 年前
        1
  •  2
  •   jantimon    16 年前

    我现在可以修了。 只要输入字段没有附加到DOM,就可以更改名称,并且单选按钮再次正常工作。

    // Old Code
     $("div:last").clone(true).children("input").attr("name","newName");
    
    // New Code
     $("div:last").clone(true).children("input").fixCloneBug ("newName");
    

    为了只降低jquery事件的执行时间,将复制classname和type属性。

    FixCloneBug方法:

    (function( $ )
    {
    
    
        if ( ! $.browser.msie || parseInt( $.browser.version ) > 7 )
            // NO FIX FOR IE 7+ FF WEBKIT
            $.fn.fixCloneBug = function( newName ){ return this.attr( "name", newName ) };
        else
            // FIX IE 5-7
            $.fn.fixCloneBug = function( newName )
            {
                var $cloned = $();
    
                this.each(function( )
                {
                        /* -._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._
    
                           Create a new element with className and jQuery events of the buggy element
    
                        */          
    
                        var     $elem    = $(this),
    
                                $newElem = $(document.createElement( $elem.attr('tagName') ));
    
                                // USE SAME TYPE
    
                        $newElem.attr('type', $elem.attr('type') );
    
    
                                // SET NAME
                        $newElem.attr('name', this.name );
                        $newElem.attr('name', newName );
    
                                // ADD TO DOM
    
                        $newElem.insertBefore( $elem );
    
                                // CLONE EVENTS
                        $newElem.data( "events", $elem.data("events") );
    
                                // CLASS NAME
                        $newElem.attr('className', this.className );
    
                        /* -._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._
    
                           Delete buggy element 
    
                        */
    
                        $elem.remove();
    
    
                        // Add to result
                        $cloned.push($newElem);
                })
    
                return $cloned;
    
            }
    
    }(jQuery));
    

    也许你认为 $newElem.attr('name', this.name ); 是无用的,但是它允许我使用jquery 1.4功能:

    .fixCloneBug (function(i,oldname){ return oldname+"_new" })

        2
  •  1
  •   xkeshav    15 年前

    试试这个(原始代码)

    $(some_cloned_element).each(function(i, elem){
        var newName = "yay_i_love_my_new_name";
        if ($.browser.msie === true){ // evil browser sniffing *cries*
            $(elem).replaceWith(document.createElement(
                this.outerHTML.replace(/name=\w+/ig, 'name='+newName+'_'+i)
            ));
        } else {
            $(elem).attr('name',newName+'_'+i);
        }
        $("body").append(some_cloned_element);
    });
    

    检查时间 i=50 然后 中断/退出

    更好的方法: 像数组一样使用名称 name=picture[]

    Refernece

        3
  •  0
  •   prodigitalson    16 年前

    如果您在发布表单时将这些内容作为集合进行访问,则无需更改名称-只需在括号内放置一个值,当您在服务器端获取数组时,它将为您递增:

    <input name="test[]" />

    如果您需要能够通过JS中的索引访问每一个,您只需使用 get 在选择器返回的适当集合上。或者您可以分配ID属性,例如 test_1 .

        4
  •  0
  •   Siva Gopal    15 年前

    单选按钮的简单解决方案是:

    $("div:last").find("radio").each(function(){
         var name="someNewName";
         var id="someNewId";
        if (!$.browser.msie || parseInt($.browser.version) > 7) {
          this.name = name;
          this.id=id;
        }
        else {
          (this).attr("outerHTML", "<input type=radio id=" + id + " name=" + name + " />");  
        }
    });
    

    这将改变 外层TML 以覆盖元素的HTML(单选按钮)。同样的解决方案也适用于其他控件的:find(“input”)/find(“checkbox”)。