使用追加来复制表行-想创建唯一的ID [英] Using Append to Copy Table Row - Would like to Create Unique Ids

查看:70
本文介绍了使用追加来复制表行-想创建唯一的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中包含一个表格,其中包含几行项目供用户填写 进入.我目前已对其进行设置,以便用户可以添加其他 排,以防他们需要添加更多信息.

I have a form, containing a table with rows of items for users to fill in. I currently have it set up so that the user can add an additional row in case they need to add more information.

这很棒,但是我想知道是否有一种生成方法 每个克隆字段的唯一ID.

This works wonderfully, but I'm wondering if there's a way to generate a unique id for each cloned field.

My function is currently this:

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

,并通过传入表ID的onclick调用.

and is called from an onclick passing in the table id.

表行包含以下信息:

<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:

What I would like is when I clone the row for the ids to be incremented by 1 so the next row would have ids:

txtBarnParts2,txtBarnWidth2,txtBarnRemarks2 ...等等.

txtBarnParts2, txtBarnWidth2, txtBarnRemarks2...and so on.

有没有办法做到这一点?

Is there a way to do this?

谢谢!

推荐答案

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);

这篇关于使用追加来复制表行-想创建唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆