2

I have 'ul > li' to get all of the list items, but I need to only grab the items that contain a checkbox that's checked.

So you can see this visually, it looks like this:

<ul>
    <li>
        <input type="checkbox" checked="checked">
    </li>
    <li>
        <input type="checkbox">
    </li>
    <li>
        <input type="checkbox" checked="checked">
    </li>
</ul>

I want to pull the 1st and 3rd item.

Thanks in advance.

So, I have

4

4 回答 4

6

You can use :has selector.

$('ul li:has(input[type=checkbox]:checked)')
于 2012-09-20T21:47:07.883 回答
5

why not go backwards and start with checked inputs

$('input[type=checkbox]:checked').parent()

http://jsfiddle.net/N7VFc/

and if you need context in case there are other checkboxes in the page you can specify it in the selector

$('input[type=checkbox]:checked','ul li').parent()
于 2012-09-20T21:47:48.710 回答
4

here is a solution to your question: http://jsfiddle.net/H4qba/1/

HTML code:

<ul>
  <li><input type="checkbox" checked="checked" />a</li>
  <li><input type="checkbox"/>b</li>
  <li><input type="checkbox"/>c</li>
  <li><input type="checkbox"/>d</li>
  <li><input type="checkbox" checked="checked" />e</li>
  <li><input type="checkbox" checked="checked" />f</li>
</ul>

jQuery code:

$('ul > li').has('input[checked="checked"]').css({border:'1px solid #c00'});
于 2012-09-20T21:51:12.107 回答
1

You can use this:

$("ul > li input:checked").closest('li');

DEMO

于 2012-09-20T21:50:04.743 回答