2

我有以下 HTML:

<input id="1" type="checkbox"/>
<label for="1">bla bla</label>
<br/>
<input id="2" type="checkbox"/>
<label for="2">hello hello</label>
<br/>
.
.
.
etc...

有没有办法在一次调用中选择前几个输入?换句话说,我不想全选它们并循环遍历每一个。只需选择 1 条语句,例如前 3 条。

4

7 回答 7

4

只需选择所有并根据需要切片结果,

$('input').slice(0, 3) //will return first 3 input.

演示

于 2012-04-23T21:09:59.587 回答
1

你的意思是这样吗?

var firstThree = $('#1, #2, #3')
于 2012-04-23T21:09:22.167 回答
1

这应该这样做

$("input:lt(3)")
于 2012-04-23T21:09:40.590 回答
1

jQuery 函数slice允许您仅指定结果的子集:

$('input').slice(0, 3)

Where 0 is the first index and 3 is the first index not included, so 2 is the last index and you get the three items.

于 2012-04-23T21:12:40.087 回答
0

干得好:

   $('input').filter(function(index) {
       return index < 3;        
    });

您可以将其更改为3您想要的任意数量的项目。

于 2012-04-23T21:09:12.977 回答
0

您可以使用 jQuery:gt:lt选择器:

$('input:lt(3)').css('blah, 'blah');

这应该选择页面上的前三个输入标签。

于 2012-04-23T21:09:33.087 回答
0

您可以使用:lt选择器。

http://api.jquery.com/lt-selector/

于 2012-04-23T21:09:46.573 回答