简体   繁体   English

在jQuery中更改克隆子项的ID

[英]Change id of clone children in jQuery

I have a table on which I have a hidden line that I clone in order to add new lines dynamically. 我有一个表,在该表上我克隆了一条隐藏线,以便动态添加新行。

var $clone = $('#table-invoicing').find('tr.hide').clone(true).removeClass('hide');
$('#table-invoicing').find('table').append($clone);

Each line have a id and a data-type. 每行都有一个id和一个数据类型。 The hidden line is set an id ending in 99. I would like to change this id when I clone the hidden line. 隐藏线设置的ID以99结尾。克隆克隆线时,我想更改此ID。

I found similar topics, but for some reason I don't manage to include it in my script. 我找到了类似的主题,但是由于某种原因,我无法将其包含在脚本中。 When I clone the line, then there is 2 elements with same id, so a selector by id won't work. 当我克隆线时,有2个具有相同ID的元素,因此按ID进行选择的选择器将不起作用。

I tried : 我试过了 :

$clone.$('#invoicing-row-descr-99').attr("id", "newID");

but then it tells me that $clone is not a function. 但是然后它告诉我$ clone不是一个函数。

Any idea ? 任何想法 ?

$clone.$('#invoicing-row-descr-99').attr("id", "newID");

but then it tells me that $clone is not a function. 但是然后它告诉我$ clone不是一个函数。

Because $clone is an object. 因为$ clone是一个对象。 Just use attr or prop for the cloned element : 只需将attrprop用于克隆的元素

$clone.attr("id", "newID");//change cloned element id

As per your comment, use like this: 根据您的评论,使用像这样:

$clone.find('your_element').attr("id", "newID");

.prop() Is a good practice in current versions of jQuery. .prop()在当前版本的jQuery中是一种很好的做法。

$clone.prop("id", "yourId");

You'll need to use it before you are appending it. 在添加它之前,您需要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM