简体   繁体   English

如何 select Jquery onclick 第一个元素中的所有嵌套输入同级

[英]How to select all nested input siblings in Jquery onclick first element

I need to Checks all input those are inside labels on click "check-all" class.我需要在点击“check-all”class 时检查标签内的所有输入。 I have tried by nextAll() method but it's not working.我已经尝试过 nextAll() 方法,但它不起作用。 kindly help好心的帮助

 $('.check-all').click(function() { alert('asdasd'); $(this).nextAll('input').prop('checked', 'true'); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="toggle-check-group"> <a><img src="img/select_all.png" class="check-all" /></a> <label class="switch"> <input type="checkbox" class="myinput" /> <span class="slider">Ankit</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Mohal</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Kamal</span> </label> </div> <div class="toggle-check-group"> <a><img src="img/select_all.png" class="check-all" /></a> <label class="switch"> <input type="checkbox" class="myinput" /> <span class="slider">Ankit</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Mohal</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Kamal</span> </label> </div>

you can:你可以:

  • use closest method to get closest parent of check-all with class toggle-check-group使用最接近的方法通过 class 获取最接近的父级
  • use find method to get all input child of toggle-check-group使用 find 方法获取 toggle-check-group 的所有输入子项

if you want to check/uncheck all check bow with the.check-all elements如果你想用 .check-all 元素检查/取消选中所有检查弓

an idea can be to store the checked state of the first check box and apply it to all sibling ones一个想法可以是存储第一个复选框的选中 state 并将其应用于所有同级复选框

var isCheck = inputs.first().prop('checked');

 $('.check-all').click(function() { var inputs = $(this).closest('.toggle-check-group').find('input'); var isCheck = inputs.first().prop('checked'); inputs.prop('checked',;isCheck) })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="toggle-check-group"> <a><img src="img/select_all.png" class="check-all" /></a> <label class="switch"> <input type="checkbox" class="myinput" /> <span class="slider">Ankit</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Mohal</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Kamal</span> </label> </div> <div class="toggle-check-group"> <a><img src="img/select_all.png" class="check-all" /></a> <label class="switch"> <input type="checkbox" class="myinput" /> <span class="slider">Ankit</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Mohal</span> </label> <label class="switch"> <input type="checkbox"/> <span class="slider">Kamal</span> </label> </div>

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

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