我有一个 html 表单中的表格,其中包含一个表格,每次单击按钮时都会添加一行。添加每一行时,我希望将新行(有四个)中每个字段的名称和ID从第一行的“Id.0”更新为第二行的“Id.1”,然后“ Id.2" 在第三个等。我可以用 id 来完成这个,使用没有问题
var next=$("#QualificationRequirements tr:last").clone();
fixIds(next,nexdex);
nexdex++;
$("#QualificationRequirements").append(next);
function fixIds(element, counter) {
$(element).find("[id]").add(element).each(function() {
this.id = this.id.replace(/\d+$/, "")+counter;
}
}
但是我发现相同的逻辑不适用于 name 属性,并且
$(element).find("[name]").add(element).each(function() {
this.name = this.name.replace(/\d+$/, "")+counter;
});
导致错误说我不能在空对象上调用“替换”,因此似乎找不到名称属性。
谁能看到我做错了什么?谢谢!