1

例如我有以下内容:

<div class="both">
    <textarea data-id="1" name="t1">Value 1</textarea>
    <input type="checkbox" value="checkbox1" checked="checked" name="c1">
    <input type="checkbox" value="checkbox1-2"  name="c1">
    <input type="checkbox" value="checkbox1-3"  name="c1">
</div>
<div class="both">
    <textarea data-id="2" name="t2">Value 2</textarea>
    <input type="checkbox" value="checkbox2-1"  name="c2">
    <input type="checkbox" value="checkbox2" checked="checked" name="c2">
    <input type="checkbox" value="checkbox2-3" name="c2">
</div>
<div class="both">
    <textarea data-id="3" name="t3">Value 3</textarea>
    <input type="checkbox" value="checkbox3-1"  name="c3">
    <input type="checkbox" value="checkbox3-2" name="c3">
    <input type="checkbox" value="checkbox3-3" checked="checked" name="c3">
</div>

我想找出两者的第一个 div 元素中复选框的数量。

愚蠢的问题,但我是新来的..

谢谢

4

3 回答 3

4
$('div.both:first input:checkbox').length

更多信息:http ://api.jquery.com/category/selectors/

于 2012-06-14T16:27:53.133 回答
3
$('div.both:first input[type="checkbox"]').length
于 2012-06-14T16:27:22.070 回答
2
$('div.both:first input[type="checkbox"]').length

正如 jQuery Doc 所说的那样http://api.jquery.com/checkbox-selector/

input[type="checkbox"]好于:checkbox

因为 :checkbox 是一个 jQuery 扩展而不是 CSS 规范的一部分,所以使用 :checkbox 的查询不能利用原生 DOM querySelectorAll() 方法提供的性能提升。为了在现代浏览器中获得更好的性能,请改用 [type="checkbox"]。

于 2012-06-14T16:28:17.233 回答