[英]JQuery not looping through elements again
I have the following code: 我有以下代码:
for(counter = 0; counter < 2; counter++) {
$form = $("<form></form>");
$form.append($('.mandatory-form-input'));
if(counter == 0) {
$form.append($('.upload_box:lt(10)'));
} else {
var start_point = (counter * 10) - 1;
$form.append($('.upload_box:gt('+ start_point +'):lt(10)'));
}
console.log($form.html());
}
The logic I want to implement is that it should create a form, add all the mandatory inputs and then add inputs on parts (based on css, lt
, gt
thing)... 我要实现的逻辑是,它应该创建一个表单,添加所有必需的输入,然后在零件上添加输入(基于css,
lt
, gt
事物)...
But this is not working as expected, on first iteration, it works properly, but on second iteration it does not console logs anything. 但这不能按预期方式工作,在第一次迭代中,它可以正常工作,但是在第二次迭代中,它没有控制台记录任何内容。 I am not sure why... (Atleast it should show the added
mandatory-form-input
fields that I am appending), but it is not even showing that. 我不知道为什么...(至少应该显示我要添加的添加的“
mandatory-form-input
字段),但什至没有显示。
I want to ask, is there something that I am missing in my code? 我想问一下,我的代码中缺少什么吗? (Like, a way to reset some $ reference or something?)
(例如,一种重置某些$引用之类的方法?)
Note: I tried clearing the $form variable at the end of the loop, it still didn't work... 注意:我尝试在循环结束时清除$ form变量,但仍无法正常工作...
Since you are appending the same element $('.mandatory-form-input')
, Then original element is removed from existing location and added to new $form
object. 由于您要添加相同的元素
$('.mandatory-form-input')
,因此原始元素将从现有位置中删除,并添加到新的$form
对象中。
You can .clone()
targeted element and then append them 您可以
.clone()
目标元素,然后附加它们
Create a deep copy of the set of matched elements.
创建一组匹配元素的深层副本。
for(counter = 0; counter < 2; counter++) {
$form = $("<form></form>");
$form.append($('.mandatory-form-input').clone()); //Affected line
....
console.log($form.html());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.