我有一个html代码,其中分别有两个父复选框和它们的子复选框。选中父项复选框时,应选中该父项的子项。就我而言,如果我选中一个父复选框,则会选中所有子复选框。我知道它的类名问题,我可以用两个不同的类名命名并解决问题。但是我不能更改类名。应该是一样的。我该如何解决??
index.html
Parent1<input name="parent" class="pc-box" type="checkbox">
Child 1<input class="check" name="child" type="checkbox">
Child 2<input class="check" name="child" type="checkbox">
Child 3<input class="check" name="child" type="checkbox">
<br>
Parent2<input name="parent" class="pc-box" type="checkbox" >
Child 1 <input class="check" name="child" type="checkbox">
Child 2 <input class="check" name="child" type="checkbox">
Child 3 <input class="check" name="child" type="checkbox">
index.js
$(document).ready(function() {
$(".pc-box").click(function() {
if (this.checked) {
('.check').prop('checked',$(this).prop('checked'));
}
});
});
您可以nextUntil
用来获取父级之后的子级复选框。如果删除该if
语句,则还可以使父项处于切换状态。
$('.pc-box').change(function() {
$(this).nextUntil('.pc-box').prop('checked', $(this).prop('checked'));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句