最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - can we use a .each within a .each in jquery? - Stack Overflow

programmeradmin0浏览0评论
$("[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 ?

Update:

The exact jsp code is as follows:

<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:

$("[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?

$("[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 ?

Update:

The exact jsp code is as follows:

<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:

$("[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?

Share Improve this question edited Sep 16, 2011 at 0:31 Bert F 87.5k12 gold badges113 silver badges126 bronze badges asked May 17, 2011 at 15:52 user735566user735566 4712 gold badges5 silver badges6 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 13

It would iterate over all elements 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 find in the outer loop in order to scope the inner loop:

$("[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:

$("[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 i in 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.

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.

It would loop through every element with that class.

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

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

        // do something

    });

});
发布评论

评论列表(0)

  1. 暂无评论