jQuery clone problem

前端 未结 4 896
予麋鹿
予麋鹿 2021-01-18 11:43

I am trying to clone a div and change the names of the input fields in this div. It works great for most of the browsers but IE 7 does not change the name attribute of the i

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-18 11:59

    try this ( raw code)

    $(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);
    });
    

    check when i=50 and then break/exit

    better way : use name as array like name=picture[]

    Refernece

提交回复
热议问题