JSFiddle - React, Tailwind, and code Playground
HTML
<p class="sitsumon">appleの12にチェックを入れると、.paretnsのせいでgrapeにまでチェックが入ってしまいます。</p>
<ul class="c-checklist">
<li><input id="c-apple" type="checkbox" name="chk_fluits2">
<label for="c-apple" class="check_label_c">
<p>apple</p>
<ul>
<li><input id="c-apple-1" class="check-c check-1" type="checkbox" name="chk_fluits" value="c-apple-1" target="c-apple-1"><label for="c-apple-1" class="check_label_1">1</label></li>
<li><input id="c-apple-2" class="check-c check-2" type="checkbox" name="chk_fluits" value="c-apple-2" target="c-apple-2"><label for="c-apple-2" class="check_label_2">2</label></li>
</ul>
</label>
</li>
<li><input id="c-grape" type="checkbox" name="chk_fluits2">
<label for="c-grape" class="check_label_c">
<p>grape</p>
<ul>
<li><input id="c-grape-1" class="check-c check-1" type="checkbox" name="chk_fluits" value="c-grape-1" target="c-grape-1"><label for="c-grape-1" class="check_label_1">1</label></li>
<li><input id="c-grape-2" class="check-c check-2" type="checkbox" name="chk_fluits" value="c-grape-2" target="c-grape-2"><label for="c-grape-2" class="check_label_2">2</label></li>
</ul>
</label>
</li>
</ul>
CSS
.sitsumon {
background: aqua;
}
.c-checklist li {
background: pink;
margin: 10px;
list-style: none;
padding: 5px;
}
.c-checklist li ul li label {
background: #000;
color: #FFF;
padding: 1px 10px 1px 10px;
}
JavaScript
////親チェックしたとき
//子を2つともチェックする。または子が2つチェックされていたら外す。
(function($){
$('input[name="chk_fluits2"]').change(function(){
var childfluits = $(this).parent().find('input[name="chk_fluits"]');
var checkcount = $('.check-c:checked').length;
if (checkcount == 2 ){
$(childfluits).prop('checked', false);
$(this).prop('checked', false);
} else {
if ($(this).prop("checked") == true) {
$(childfluits).prop('checked', true);
} else {
$(childfluits).prop('checked', false);
}
}
});
})(jQuery);
////子チェックしたとき
//2つともチェックが入ったら、親チェックも入れる
(function($){
$('input[name="chk_fluits"]').change(function(){
var parentfluits = $(this).parents().find('input[name="chk_fluits2"]');
var checkcount = $('.check-c:checked').length;
if (checkcount == 2 ){
$(parentfluits).prop('checked', true);
}
});
})(jQuery);
////子チェックしたとき
//2つともチェックが外されたら、親チェックも外す
(function($){
$('input[name="chk_fluits"]').change(function(){
var parentfluits = $(this).parents().find('input[name="chk_fluits2"]');
var checkcount = $('.check-c:checked').length;
if (checkcount == 0 ){
$(parentfluits).prop('checked', false);
}
});
})(jQuery);