gpt4 book ai didi

jquery - 使用追加复制表行 - 想要创建唯一 ID

转载 作者:行者123 更新时间:2023-12-01 00:08:56 29 4
gpt4 key购买 nike

我有一个表单,其中包含一个表格,其中包含供用户填写的行项目 我目前已将其设置为用户可以添加额外的 行,以防他们需要添加更多信息。

这非常有效,但我想知道是否有办法生成 每个克隆字段的唯一 ID。

My function is currently this:

function addTableRow(table) {
$(table).append($(table + ' tr:last').clone());
return true;
}

并通过传入表 ID 的 onclick 调用。

表格行包含以下信息:

<tr>
<td><input type="text" id="txtBarnParts1" name="txtBarnParts
[]"></td>
<td><input type="text" id="txtBarnWidth1" name="txtBarnWidth
[]">ft</td>
<td><input type="text" id="txtBarnRemarks1"
name="txtBarnRemarks1[]"></td>
</tr>

我想要的是当我克隆 id 的行时 加 1,因此下一行将有 id:

txtBarnParts2、txtBarnWidth2、txtBarnRemarks2...等等。

有办法做到这一点吗?

谢谢!

最佳答案

var $newTr = $(table + ' tr:last').clone();
var newIndex = $newTr.index() + 1;
$newTr.find('[id]').each(function () {
this.id = this.id.replace(/[0-9]+$/e, newIndex);
});
$(table).append($newTr);

关于jquery - 使用追加复制表行 - 想要创建唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3503279/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com