Jquery 选中复选框时更改标签

Jquery 选中复选框时更改标签,jquery,html,css,checkbox,label,Jquery,Html,Css,Checkbox,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="filte

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

<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>
CSS:


您当前试图调用
输入
元素上的
切换类
,而不是
标签
。您可以使用获取
标签

$(".filteritem").click(function(){
    $(this).parent().toggleClass('highlight');
});
HTML:

现场演示:


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

是的,这是一个很好的解决方案。简单但有效。
.highlight {
    //change something
}
$(".filteritem").on('change', function(){
     $(this).closest('label').toggleClass('highlight');
});
$(".filteritem").click(function(){
    $(this).parent().toggleClass('highlight');
});
<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>
$( '.checkboxlist' ).on( 'click', 'input:checkbox', function () {
   $( this ).parent().toggleClass( 'highlight', this.checked );
});