简体   繁体   English

如何在单击父复选框时取消选中子复选框

[英]How to unchecked child checkbox on click parent checkbox

Hello I need to check and uncheck child checkboxes on click parent checkboxes, Right now I'm able to check child checkboxes but not able to uncheck those by click on the same, how to achieve this.您好,我需要在单击父复选框上选中和取消选中子复选框,现在我可以选中子复选框但无法通过单击相同的方式取消选中这些复选框,如何实现这一点。

my code我的代码

HTML HTML

 <ul class="theme-list-p">
                          <li>
                            <input type="checkbox" name="">&nbsp;&nbsp;Ambah
                            <ul class="theme-list-c">
                              <li><input type="checkbox" name="">&nbsp;&nbsp;Kirnapur</li>
                              <li><input type="checkbox" name="">&nbsp;&nbsp;Barwani</li>
                              <li><input type="checkbox" name="">&nbsp;&nbsp;Sihora</li>
                            </ul>
                          </li>
                          <li><input type="checkbox" name="">&nbsp;&nbsp;Sabalgarh
                            <ul class="theme-list-c">
                              <li><input type="checkbox" name="">&nbsp;&nbsp;Village1</li>
                              <li><input type="checkbox" name="">&nbsp;&nbsp;Village2</li>
                              <li><input type="checkbox" name="">&nbsp;&nbsp;Village3</li>
                            </ul>
                          </li>
                          <li><input type="checkbox" name="">&nbsp;&nbsp;Kailaras</li>
                        </ul>

CSS CSS

.theme-list-p{ . 主题列表-p{

  list-style: none;
  padding-left: 10px;
  font-weight: bold;
  border:1px solid #dee2e6;
  padding: 5px;
  overflow-y: scroll;
  min-height: 120px;
}

.theme-list-c{
  list-style: none;
  padding-left: 20px;
  font-weight: 400;
}

jquery jquery

<script type="text/javascript">
  $(document).on('change', '.theme-list-p li', function(){
     $(this).find('.theme-list-c li input').attr('checked', true);
  });
</script>

You could do it like this:你可以这样做:

$(document).on('change', '.theme-list-p li input', function() {
  $(this).next('.theme-list-c').find('li input').attr('checked', $(this).is(":checked"));
});

Demo演示

 $(document).on('change', '.theme-list-p li input', function() { $(this).next('.theme-list-c').find('li input').attr('checked', $(this).is(":checked")); });
 .theme-list-p { list-style: none; padding-left: 10px; font-weight: bold; border: 1px solid #dee2e6; padding: 5px; overflow-y: scroll; min-height: 120px; }.theme-list-c { list-style: none; padding-left: 20px; font-weight: 400; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="theme-list-p"> <li> <input type="checkbox" name="">&nbsp;&nbsp;Ambah <ul class="theme-list-c"> <li><input type="checkbox" name="">&nbsp;&nbsp;Kirnapur</li> <li><input type="checkbox" name="">&nbsp;&nbsp;Barwani</li> <li><input type="checkbox" name="">&nbsp;&nbsp;Sihora</li> </ul> </li> <li><input type="checkbox" name="">&nbsp;&nbsp;Sabalgarh <ul class="theme-list-c"> <li><input type="checkbox" name="">&nbsp;&nbsp;Village1</li> <li><input type="checkbox" name="">&nbsp;&nbsp;Village2</li> <li><input type="checkbox" name="">&nbsp;&nbsp;Village3</li> </ul> </li> <li><input type="checkbox" name="">&nbsp;&nbsp;Kailaras</li> </ul>

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

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