[英]How to use jQuery to dynamically update IDs after cloning a table row?
触发后,将克隆表的第3行(底部)。 在CSS中,该行(#tr3)设置为:display:none; 这是我要克隆的表行:
<tr name="tr3" id="tr3">
<td><input type="text" name="dt1" id="dt1"></td>
<td><input type="text" name="fn1" id="fn1"></td>
<td><a href="#" name="change1" id="change1">change</a></td>
<td><a href="#" name="del1" id="del1">delete</a></td>
</tr>
这是克隆行的JQuery代码。 不幸的是,不是我的,所以我不了解全部。
$("table tr:nth-child(4)").clone().find("input").each(function() {
$(this).val('').attr('id', function(_, id) {
return id + count;
});
具体来说,第2行中的功能如何工作-下划线是什么?
不过,这就是我的目的。 我怎样才能:
谢谢。
为了清楚起见:
var $clone = $("table tr:nth-child(4)").clone();
如何将样式更改为display:block
克隆行?
$clone.show();
如何在克隆的行中更新锚标记的ID。
$clone.find("a, input").each(function() { $(this).val('').attr('id', function(_, id) { return id + count; }); });
在上面的jQuery代码中,第2行中的函数如何工作-下划线是什么?
下划线( _
)是有效的ECMAScript的标识符名称 ,它仅用作占位符以获取第二个参数( id
。
以及所有的一切(未经测试)。
$("table tr:nth-child(4)")
.clone()
.show()
.find("a, input").each(function() {
$(this).val('').attr('id', function(_, id) {
return id + count;
});
});
您实际上从未真正访问过克隆的行。 您将不得不将其分配给某些内容,例如
$clonedRow = $("table ...").clone();
然后您可以运行所有其他方法,最后将$clonedRow
附加到DOM,可能是
$clonedRow.insertAfter("table ...")
您可能需要使用正则表达式以某种方式去除最后一位:
return id.replace(/\\d+$/, '') + count;
_
只是一个占位符。 它没有使用,因此代码的编写者选择了一个非描述性的变量名,因为声明中需要某些内容才能获取id
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.