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 input fields.
// 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));
I could fix it for now.
As long as an input field is not attached to the dom you can change the name and the radio buttons work properly again.
// Old Code
$("div:last").clone(true).children("input").attr("name","newName");
// New Code
$("div:last").clone(true).children("input").fixCloneBug ("newName");
To lower the execution time only the jQuery Events, the className and the type attribute are copied.
fixCloneBug Method:
(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));
Maybe you think $newElem.attr('name', this.name ); is useless however it allows me to use a jQuery 1.4 feature:
如果您需要能够通过 js 中的索引访问每一个,您可以在返回的相应集合上使用 get通过选择器。或者您可以分配 ID 属性,例如 test_1。
If you pan on accessing these as a set when the form is posted then there is no need to change the name - just dont put a value within the brackets and it will be incremented for you when you grab the array on the server side:
<input name="test[]" />
If you need to be able to access each one by index from js you can just use get on the appropriate collection returned by a selector. Or you can assign ID attributes like test_1.
$("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 + " />");
}
});
This will alter the outerHTML of the element so that the HTML for the element(radio button) is overwritten. The same solution can be applied for : find("input") / find("checkbox") for the other controls too.
发布评论
评论(4)
我现在可以修复它。
只要输入字段未附加到 dom,您就可以更改名称并且单选按钮再次正常工作。
为了降低执行时间,仅复制 jQuery 事件、className 和 type 属性。
fixCloneBug 方法:
也许您认为
$newElem.attr('name', this.name );
无用,但它允许我使用 jQuery 1.4 功能:.fixCloneBug (function(i,旧名称){ 返回旧名称+“_new” })
I could fix it for now.
As long as an input field is not attached to the dom you can change the name and the radio buttons work properly again.
To lower the execution time only the jQuery Events, the className and the type attribute are copied.
fixCloneBug Method:
Maybe you think
$newElem.attr('name', this.name );
is useless however it allows me to use a jQuery 1.4 feature:.fixCloneBug (function(i,oldname){ return oldname+"_new" })
尝试这个(原始代码)
检查i=50然后中断/退出
更好的方法:使用名称作为数组,如
name=图片[]
参考< /a>
try this ( raw code)
check when i=50 and then break/exit
better way : use name as array like
name=picture[]
Refernece
如果您在发布表单时将它们作为一组进行访问,则无需更改名称 - 只需不要在括号内放置值,当您在服务器端获取数组时,它会为您递增:
< code>
如果您需要能够通过 js 中的索引访问每一个,您可以在返回的相应集合上使用
get
通过选择器。或者您可以分配 ID 属性,例如test_1
。If you pan on accessing these as a set when the form is posted then there is no need to change the name - just dont put a value within the brackets and it will be incremented for you when you grab the array on the server side:
<input name="test[]" />
If you need to be able to access each one by index from js you can just use
get
on the appropriate collection returned by a selector. Or you can assign ID attributes liketest_1
.单选按钮的简单解决方案是:
这将更改元素的 outerHTML,以便覆盖元素(单选按钮)的 HTML。同样的解决方案也适用于其他控件:find("input") / find("checkbox")。
The simple solution for Radio button is :
This will alter the outerHTML of the element so that the HTML for the element(radio button) is overwritten. The same solution can be applied for : find("input") / find("checkbox") for the other controls too.