javascript 我们可以在 jquery 的 .each 中使用 .each 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6033613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:14:06  来源:igfitidea点击:

can we use a .each within a .each in jquery?

javascriptjqueryeach

提问by user735566

$("[name=form_1]").each(function(){
    alert("i in else"+i);
    $('.eform_text').each(function() {
    });
});

would this loop iterate over all elements that have a class of eform_text only in form1 .Or would it iterate over all elements which have that class ?

这个循环是否会迭代所有具有 eform_text 类的元素只在 form1 中。或者它会迭代所有具有该类的元素?

Update:

更新:

The exact jsp code is as follows:

具体的jsp代码如下:

<c:when test="${eformDetails.controlType==1}"> <input id="textBox_${eformDetails.id}_${eformDetails.required}_${i}" class="eformDetail eform_text" type="text" value="" name="form_${i}" onblur="validateEformInputs(${i-1})"></input> </c:when>

<c:when test="${eformDetails.controlType==1}"> <input id="textBox_${eformDetails.id}_${eformDetails.required}_${i}" class="eformDetail eform_text" type="text" value="" name="form_${i}" onblur="validateEformInputs(${i-1})"></input> </c:when>

i have the form which varies each time.and for each form i need to obtain all the text boxes.Currently after your help my javascript is ass follows:

我的表单每次都不同。对于每个表单,我需要获取所有文本框。目前在您的帮助下,我的 javascript 如下:

$("[name=form_"+i+"]").each(function(i){ alert("i in else"+i);

$("[name=form_"+i+"]").each(function(i){ alert("i in else"+i);

         $('.eform_text', this).each(function() {
        textboxId = $(this).attr("id");

It reaches the first alert but i am not able to reach the second loop.It is not obtaining elements that have class eform_text.Not sure what is going wrong here.Could you please help?

它到达第一个警报,但我无法到达第二个循环。它没有获取具有类 eform_text 的元素。不确定这里出了什么问题。你能帮忙吗?

回答by T.J. Crowder

It would iterate over allelements with that class, whether inside a form with the name "form_1" or not. To only look within each form (I'm guessing you must have more than one form with the name "form_1", though that seems odd), use findin the outer loop in order to scope the inner loop:

它将迭代具有该类的所有元素,无论是否在名称为“form_1”的表单中。要仅查看每个表单(我猜您必须有多个名为“form_1”的表单,尽管这看起来很奇怪),请find在外循环中使用以限定内循环的范围:

$("[name=form_1]").each(function(formIndex) {
    alert("formIndex in each: " + formIndex);
    $(this).find('.eform_text').each(function(textIndex) {
        alert("textIndex in each: " + textIndex);
    });
});

Or you can use the second argument to $(), which provides the context in which to work:

或者您可以使用第二个参数 to $(),它提供了工作的上下文:

$("[name=form_1]").each(function(formIndex) {
    alert("formIndex in each: " + formIndex);
    $('.eform_text', this).each(function(textIndex) {
        alert("textIndex in each: " + textIndex);
    });
});

Either should work.

要么应该工作。

Note that as @Shrikant Sharat pointed out in the comments (thanks Shrikant!), I've assumed the iin your original code is meant to be the index that gets passed into each. I've shown the indexes at both levels (with descriptive names) above.

请注意,正如@Shrikant Sharat 在评论中指出的(感谢 Shrikant!),我假设i您的原始代码中的 是传递到each. 我已经显示了上面两个级别的索引(带有描述性名称)。

回答by rockerest

Your second answer.

你的第二个答案。

Because you're calling $(each time, it instantiates a new copy of the jQuery object which doesn't care what level of a function it's in.

因为您$(每次都在调用,它会实例化 jQuery 对象的一个​​新副本,该副本并不关心它在函数的哪个级别。

It would loop through every element with that class.

它将遍历具有该类的每个元素。

回答by Jean Michell

$('.element').each(function(){

    $(this).find('.elementChild').each(function(){

        // do something

    });

});