简体   繁体   English

单击复选框将类添加/删除到父元素

[英]Adding/removing class to parent element while clicking on checkbox

I have simple list with <input type='checkbox'> inside every li . 我在每个li内部都有一个带有<input type='checkbox'>简单列表。 Few of them are checked and other not 他们很少checked ,其他没有

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

How can I add class .transparent to those li that contains <input type='checkbox'> that is not checked and remove this class when user will mark <input type='checkbox'> as checked ? 我如何在未包含<input type='checkbox'> li中添加类.transparent ,并在用户将<input type='checkbox'>标记为已checked时删除此类?

The main problem is that I want to add/remove this class (1) when page is loading and (2) change it later while user will click something on my list. 主要问题是,我想添加/删除此类(1)在页面加载时,以及(2)以后进行更改,而用户将单击列表中的某些内容。

It can be done with some JavaScript: 可以使用一些JavaScript来完成:

 function formatCheckboxes(){ jQuery('li').each(function( index ) { jQuery(this).addClass('transparent'); jQuery(this).has('input:checked').removeClass('transparent'); }); } jQuery(document).ready(function(){ formatCheckboxes(); }); jQuery(document).on('change','input[type="checkbox"]', function(){ formatCheckboxes(); }); 
 .transparent{ opacity:0.2; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li><input type='checkbox' checked></li> <li><input type='checkbox'></li> <li><input type='checkbox' checked></li> <li><input type='checkbox'></li> </ul> 

 $(document).ready(function(){ // (1) when page is loading $("li").each(function(index) { if($(this).find('input[type="checkbox"]').is(':checked')){ $(this).addClass('transparent'); } }); }); //(2) change it later while user will click something on my list. $(document).on('change',"ul li input[type=checkbox]",function(){ $(this).parent().toggleClass('transparent'); }); 
 .transparent{ color:red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li><input type='checkbox' checked></li> <li><input type='checkbox'></li> <li><input type='checkbox' checked></li> <li><input type='checkbox'></li> </ul> 

Loop thru each checkboxes and check whether they are checked/unchecked and based on that add transparent class to the parent li : 遍历每个复选框,并检查是否已选中/未选中它们,并基于向父li添加transparent类的方式:

 $(document).ready(function() { $('input[type="checkbox"]:checked').each(function() { $(this).parent().addClass('transparent'); }); }); $('input[type=checkbox]').on('change', function() { $(this).parent().toggleClass('transparent'); }); 
 .transparent { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li><input type='checkbox' checked></li> <li><input type='checkbox'></li> <li><input type='checkbox' checked></li> <li><input type='checkbox'></li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM