javascript - jQuery - find if a child input element is checked without referencing its id explicitly? - Stack Overflow

Given the code below it displays two check-boxes and I want to find if the first input (or checkbox) ha

Given the code below it displays two check-boxes and I want to find if the first input (or checkbox) has the 'checked' attribute set to 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>

Without referencing the id of the first checkbox how can I get jQuery to see if the first list item is checked?

ex below:

$('#checklist).child('li label first:input').is(':checked');

Given the code below it displays two check-boxes and I want to find if the first input (or checkbox) has the 'checked' attribute set to 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>

Without referencing the id of the first checkbox how can I get jQuery to see if the first list item is checked?

ex below:

$('#checklist).child('li label first:input').is(':checked');
Share Improve this question edited Jun 9, 2015 at 13:37 Prasanna 7795 silver badges13 bronze badges asked Jun 9, 2015 at 13:15 JebathonJebathon 4,60314 gold badges62 silver badges117 bronze badges 1
  • $('#checklist).find("(input:checked):first"); – Khanh TO Commented Jun 9, 2015 at 13:17
Add a ment  | 

3 Answers 3

Reset to default 7

You can check :

$("#checklist :checkbox:first:checked").length>0

Optimization :

To prevent jQuery search * items , and find only input type , , this should be made :

$("#checklist input:checkbox:first:checked").length>0

From jQuery :

it is remended 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.

var res = $("#checklist input:eq(0)").is(function() {
  return this.checked
});

console.log(res);
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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>

TO get the first checked checkbox

  1. Use find because checkbox is not the direct child of ul#checklist
  2. Use :checkbox pseudo-selector to get all checkboxes
  3. Use first to get first checkbox
  4. Use is(':checked') to get the checked status

    $('#checklist').find(':checkbox').first().is(':checked');

OR

$('#checklist :checkbox:first').is(':checked');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744190610a4562416.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信