我有一個html表格,其中包含一個表格,每次單擊按鈕時都會添加一行。當添加每一行時,我希望新行中的每個字段的名稱和id(有四個)從第一行中的「Id.0」更新爲第二行中的「Id.1」,然後「 ID.2" 第三,等我可以通過ID做到這一點使用更改附加表格行字段的名稱jQuery
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;
});
沒問題
會導致一個錯誤,表示我無法在空對象上調用「替換」,因此看起來名稱屬性未找到。
任何人都可以看到我做錯了什麼?謝謝!
每當你得到一個錯誤,請提供。 –