2011-07-09 55 views

回答

3
 $('#selectDropDowns select').each(function(i) { 

      // do usual stuff 
      if (i==3) 
      { 
       // do extra stuff only if this is the 4th iteration 
      } 

     }); 
+1

这是第5次迭代。基于零指数。 – epascarello

+0

@epascarello:确定...已修复。 – yankee

1

像这样:

$('#selectDropDowns select').each(function(index, element) { 
    // index represents the current index of the iteration 
    // and element the current item of the array   
}); 
0
$('#selectDropDowns select').each(function(index) { 

    // do usual stuff 
    if(index ==3){ 
    // do extra stuff only if this is the 4th iteration 
    } 
}); 

工作例如:http://jsfiddle.net/SgMuJ/1/

+1

为什么这么“复杂”? –

+0

@Felix King不知道你可以通过索引,只是创建一个小提琴很好地工作。这就是为什么我喜欢这个网站,我每天都会学到新东西 –

+0

这是第5次迭代。基于零指数。 – epascarello

2

传递给each(..)可以取两个参数的函数 - 索引和元素。这就是你看到的第一件事,当你打开文件:

.each(function(index, Element)) 

所以:

$('#selectDropDowns select').each(function(i) { 
     if (i == 3) ... 
}); 
+0

这是第5次迭代。基于零指数。 – epascarello

+0

当然,更正 – Bozho

0

使用$(这)...

$('#selectDropDowns select').each(function(i, val) { 
     //Zero-index based thus to grab 4th iterator -> index = 3 
     if (i == 3) { 
      alert($(this).attr('id')); 
     } 

    } 

请注意,你也可以在.each函数声明中获取索引和元素的值。

+0

这是第5次迭代。基于零指数。 – epascarello

0

如果您不需要循环使用,则可以使用eq()

$('#selectDropDowns select').eq(3); 
相关问题