[英]replace id attribute in for loop
I asked another question on this , and got an answer , but still managed to mess it up. 我问了另一个问题,并得到了答案,但仍然设法搞砸了。 I have a for loop , and I want to make clones of a template in my for loop. 我有一个for循环,我想在我的for循环中制作模板的克隆。 I would like each id to be replaced with the id + 0 the first time through the loop. 我想首次通过循环将每个id替换为id + 0。 So textbox with id tFirstName
would be tFirstName0
and id tLastName
would be tLastName0
and so on... Then for the next clone , then next time through the loop it would be tFirstName1 , tLastName1, ect.. 因此,具有id tFirstName
文本框将是tFirstName0
并且id tLastName
将是tLastName0
,依此类推......然后对于下一个克隆,然后下一次循环它将是tFirstName1,tLastName1,等等。
The problem with this code is that i
is adding one for every text box, so in the first template the id's are tFirstName0
, tLastName1
, ect.. 这段代码的问题是i
为每个文本框添加一个,所以在第一个模板中,id是tFirstName0
, tLastName1
,等等。
I am looking for - if someone has a suggestion to keep i
uniform through out the for loop, then increase , then stay uniform through the next loop 我正在寻找 - 如果有人建议让i
在整个for循环中保持统一,那么增加,然后通过下一个循环保持统一
var NumofClones = (4 * 1);
for (i = 0; i < NumofClones; i++) {
var newrow = $('._template').clone().removeClass('_template');
newrow.find('input[type=text]').attr('id', function (i, oldID) {
return oldID + i
});
$('.placenewrows').append(newrow);
}
The i
in the .attr()
's callback function refer to the parameter i
, not the i
in the for loop, change the parameter i
of the callback function to some others. 在i
在.attr()
的回调函数参考参数i
,而不是i
在for循环,改变参数i
的回调函数,还有一些其它的。
var NumofClones = (4 * 1);
for (var i = 0; i < NumofClones; i++) {
var newrow = $('._template').clone().removeClass('_template');
newrow.find('input[type=text]').attr('id', function (index, oldID) {
return oldID + i;
});
$('.placenewrows').append(newrow);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.