选中复选框后更改标签

use*_*906 0 html css checkbox jquery label

我有很多带有复选框的列表,如下所示:

<ul>
   <li><label class="highlight"><input type="checkbox" id="1" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="223" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="32" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="42" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="54" class="filteritem">Lorem Ipsum</label</li>
</ul>

<ul>
   <li><label class="highlight"><input type="checkbox" id="43" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="343" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="342" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="53" class="filteritem">Lorem Ipsum</label</li>
   <li><label class="highlight"><input type="checkbox" id="55" class="filteritem">Lorem Ipsum</label</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

每个复选框都有一个唯一的ID

我想在checkt时切换标签的背景颜色.

这是我得到的,但它不起作用:

jQuery的:

$(".filteritem").click(function(){
    $(this).toggleClass('highlight');
});
Run Code Online (Sandbox Code Playgroud)

CSS:

.highlight {
    //change something
}
Run Code Online (Sandbox Code Playgroud)

Šim*_*das 7

HTML:

<ul class="checkboxlist">
   <li><label><input type="checkbox" id="1"> Lorem Ipsum</label></li>
   <li><label><input type="checkbox" id="223"> Lorem Ipsum</label></li>
   <li><label><input type="checkbox" id="32"> Lorem Ipsum</label></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

$( '.checkboxlist' ).on( 'click', 'input:checkbox', function () {
   $( this ).parent().toggleClass( 'highlight', this.checked );
});
Run Code Online (Sandbox Code Playgroud)

现场演示: http ://jsfiddle.net/MGVHX/1/

请注意,我使用事件委派,而不是将相同的处理程序绑定到每个复选框.