鉴于下面的代码它显示了两个复选框,我想知道第一个输入(或复选框)是否将“checked”属性设置为 true。
<ul id="checklist">
<li id="box1">
<label>
<input id="box_input" type="checkbox"> "A CheckBox" </label>
</li>
<li id="box2">
<label>
<input id="box_input_2" type="checkbox"> "Another CheckBox"</label>
</li>
</ul>
如果不引用第一个复选框的 ID,我如何让 jQuery 查看第一个列表项是否被选中?
例如:
$('#checklist).child('li label first:input').is(':checked');
最佳答案
你可以检查:
$("#checklist :checkbox:first:checked").length>0
优化:
为了防止 jQuery 搜索 *
项,并且只找到输入类型,,应该这样做:
$("#checklist input:checkbox:first:checked").length>0
来自 jQuery :
it is recommended to precede it with a tag name or some other selector; otherwise, the universal selector ("*") is implied. In other words, the bare
$(':checkbox')
is equivalent to$( "*:checkbox" )
, so $( "input:checkbox" ) should be used instead.
关于javascript - jQuery - 查找是否在未显式引用其 id 的情况下检查子输入元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30733103/