Jquery 使用“附加到复制表行”-要创建唯一的ID吗
我有一个表单,其中包含一个表,表中有供用户填写的项目行 在里面我目前已将其设置为用户可以添加其他 行,以防他们需要添加更多信息 这非常有效,但我想知道是否有一种方法可以生成 每个克隆字段的唯一idJquery 使用“附加到复制表行”-要创建唯一的ID吗,jquery,Jquery,我有一个表单,其中包含一个表,表中有供用户填写的项目行 在里面我目前已将其设置为用户可以添加其他 行,以防他们需要添加更多信息 这非常有效,但我想知道是否有一种方法可以生成 每个克隆字段的唯一id My function is currently this: function addTableRow(table) { $(table).append($(table + ' tr:last').clone()); return true; } 并从传
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……等等
有办法做到这一点吗
谢谢 试试这个。需要jQuery 1.4或更高版本 根据您的使用情况,我假设
表
存储了一个带有散列的ID,如“#myTable”
中所示
这会在新(克隆)行上执行一个.find()
,搜索具有ID属性的输入
元素。然后它将一个函数传递给.attr()
方法,该方法用新的结束数字替换当前的结束数字
EDIT:在
.attr()
调用中缺少一个逗号。固定的
编辑:将.clone()
放在错误的位置。固定的
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。这是通用功能
var indexCloneTableLastRowSelect = 1;
var indexCloneTableLastRowInput = 1;
var forEachElementItrateOnce = 1;
function cloneTableLastRow_(tableID_) {
tableID_ = '#' + tableID_;
// copy|clone last row
$(tableID_).append($(tableID_ + " tr:last").clone());
$(tableID_ + " tr:last").each(function (){
$(tableID_ + " tr:last select").attr("name", function() {
if (forEachElementItrateOnce == 1){
indexCloneTableLastRowSelect++;
}
this.name = this.name.replace(/[0-9]+$/, indexCloneTableLastRowSelect);
this.id = this.id.replace(/[0-9]+$/, indexCloneTableLastRowSelect);
forEachElementItrateOnce = 0;
})
$(tableID_ + " tr:last input").attr("name", function() {
if (forEachElementItrateOnce == 1){
indexCloneTableLastRowInput++;
}
this.name = this.name.replace(/[0-9]+$/, indexCloneTableLastRowInput);
this.id = this.id.replace(/[0-9]+$/, indexCloneTableLastRowInput);
forEachElementItrateOnce = 0;
})
})
forEachElementItrateOnce = 1;
}
giker-这不会删除旧的数字,因此在
txtBarnParts1
ID的情况下,下一个将是txtBarnParts12
然后txtBarnParts123
等等o) 您只需要在第一行中有没有数字的id。@giker-但您正在克隆:last
行,这意味着您正在引用附加的上一行,该行具有更新的id。您可以将:last
更改为:first
,但它假设OP想要一行没有数字。我不想要新的clonned行中的数据。有什么想法吗?我克隆了最后一行,但对于索引,我使用的是第一行,没有数字+counter@user-你是说像一个JSFIDLE示例?请稍等。@user-对不起,我出错了。我把.clone()
放错位置了。固定的。这里有一个例子。当您添加一行时,它将弹出HTML以便您可以看到ID。为了快速回复..但我希望行计数也附加输入。有什么想法吗?@user-你的意思是想用数字设置输入值吗?容易的。只需添加this.value=num
到.attr()
中的函数。如果要将其附加到当前值,请执行以下操作this.value=this.value+num代码>
var i = 1
function addTableRow() {
$("table").append($("table tr:last").clone());
$("table tr:last input").attr("name", $("table tr:first input").attr("name")+i);
i++;
})
var indexCloneTableLastRowSelect = 1;
var indexCloneTableLastRowInput = 1;
var forEachElementItrateOnce = 1;
function cloneTableLastRow_(tableID_) {
tableID_ = '#' + tableID_;
// copy|clone last row
$(tableID_).append($(tableID_ + " tr:last").clone());
$(tableID_ + " tr:last").each(function (){
$(tableID_ + " tr:last select").attr("name", function() {
if (forEachElementItrateOnce == 1){
indexCloneTableLastRowSelect++;
}
this.name = this.name.replace(/[0-9]+$/, indexCloneTableLastRowSelect);
this.id = this.id.replace(/[0-9]+$/, indexCloneTableLastRowSelect);
forEachElementItrateOnce = 0;
})
$(tableID_ + " tr:last input").attr("name", function() {
if (forEachElementItrateOnce == 1){
indexCloneTableLastRowInput++;
}
this.name = this.name.replace(/[0-9]+$/, indexCloneTableLastRowInput);
this.id = this.id.replace(/[0-9]+$/, indexCloneTableLastRowInput);
forEachElementItrateOnce = 0;
})
})
forEachElementItrateOnce = 1;
}