html元素的jQuery克隆问题
我正在尝试克隆HTML元素(文本框)&它在jQuery1.4.2中运行良好,但在我的项目中,我使用的是jQuery1.9.1 问题是,在jQuery1.9.1中,即使值设置为null,它也会克隆文本框的值 以下是: 谢谢。用于设置值,而不是使用值属性html元素的jQuery克隆问题,jquery,html,Jquery,Html,我正在尝试克隆HTML元素(文本框)&它在jQuery1.4.2中运行良好,但在我的项目中,我使用的是jQuery1.9.1 问题是,在jQuery1.9.1中,即使值设置为null,它也会克隆文本框的值 以下是: 谢谢。用于设置值,而不是使用值属性 var i = 1; $("button").click(function() { $("table tr:first").clone().find("input").each(function() { $(this).at
var i = 1;
$("button").click(function() {
$("table tr:first").clone().find("input").each(function() {
$(this).attr({
'id': function(_, id) { return id + i },
'name': function(_, name) { return name + i }
}).val('');
}).end().appendTo("table");
i++;
});
演示:如果使用最新的jquery(1.6+),请使用prop()
而不是attr()
例如,应检索selectedIndex、tagName、nodeName、nodeType、ownerDocument、defaultChecked和defaultSelected,并使用.prop()方法进行设置。在jQuery1.6之前,可以使用.attr()方法检索这些属性,但这不在attr的范围内。这些属性没有相应的属性,只是属性
试试这个
var i = 1;
$("button").click(function() {
$("table tr:first").clone().find("input").each(function() {
$(this).prop({
'id': function(_, id) { return id + i },
'name': function(_, name) { return name + i },
'value': ''
});
}).end().appendTo("table");
i++;
});
或者使用attr()
而不是使用.val(“”)
我想说您的问题可能与
.attr
vs(在1.6.x中添加)中的更改有关
var i = 1;
$("button").click(function() {
$("table tr:first").clone().find("input").each(function() {
$(this).prop({
'id': function(_, id) { return id + i },
'name': function(_, name) { return name + i },
'value': ''
});
}).end().appendTo("table");
i++;
});
var i = 1;
$("button").click(function() {
$("table tr:first").clone().find("input").each(function() {
$(this).attr({
'id': function(_, id) { return id + i },
'name': function(_, name) { return name + i }
}).val('');
}).end().appendTo("table");
i++;
});