У меня есть этот простой код jquery для флажка, который проверяет атрибуты. Я знаю, что на этот ответ уже есть много общего, но идея здесь другая.
Я хочу показать элемент A_B
div для следующего условия:
# 1: Флажок пользователя checked
A
или B
показывает элемент A_B
div
# 2: Если пользователь unchecked
A
установлен, но флажок B
все еще установлен. Затем элемент A_B
div все еще отображается.
*Note: This can be reversal for the situation*
# 3 Если пользователь снял оба флажка A
или B
. Тогда скроется только элемент A_B
div.
Прямо сейчас. Когда я снимаю галочку A
или B
. Элемент div будет скрыт, он не должен переключаться.
Есть ли здесь что улучшить. Для достижения следующих 3
условий?
Любые идеи/решения были бы большим подспорьем. Может быть полезно также для некоторых пользователей, которые сталкиваются с той же проблемой, что и моя. Спасибо.
$('input[chck-type = "a_b"]').click(function(){
if (
$(this).prop("checked") == true
) {
$('.A_B').removeClass('hide');
} else {
$('.A_B').addClass('hide');
}
});
$('input[chck-type = "c"]').click(function(){
if (
$(this).prop("checked") == true
) {
$('.C_Box').removeClass('hide');
} else {
$('.C_Box').addClass('hide');
}
});
.hide {display:none}
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p><input type = "checkbox" chck-type = "a_b">A</p>
<p><input type = "checkbox" chck-type = "a_b">B</p>
<p><input type = "checkbox" chck-type = "c">C</p>
<div class='A_B hide'>
This box is for A or B.
</div>
<div class='C_Box hide'>
This box is for C.
</div>
Вы не проверяете, сняты ли оба флажка или нет.
$('input[chck-type = "a_b"]').click(function(){
if (
$(this).prop("checked") == true
) {
$('.A_B').removeClass('hide');
} else if ($('input[chck-type = "a_b"]')[0].checked == false && $('input[chck-type = "a_b"]')[1].checked == false) {
$('.A_B').addClass('hide');
}
});
$('input[chck-type = "c"]').click(function(){
if (
$(this).prop("checked") == true
) {
$('.C_Box').removeClass('hide');
} else {
$('.C_Box').addClass('hide');
}
});
.hide {display:none}
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p><input type = "checkbox" chck-type = "a_b">A</p>
<p><input type = "checkbox" chck-type = "a_b">B</p>
<p><input type = "checkbox" chck-type = "c">C</p>
<div class='A_B hide'>
This box is for A or B.
</div>
<div class='C_Box hide'>
This box is for C.
</div>
$('input[chck-type = "a_b"]').click(function(){
let checkboxes = $('input[chck-type = "a_b"]:checked').length;
if (
checkboxes != 0
) {
$('.A_B').removeClass('hide');
} else {
$('.A_B').addClass('hide');
}
});
$('input[chck-type = "c"]').click(function(){
if (
$(this).prop("checked") == true
) {
$('.C_Box').removeClass('hide');
} else {
$('.C_Box').addClass('hide');
}
});
.hide {display:none}
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p><input type = "checkbox" chck-type = "a_b">A</p>
<p><input type = "checkbox" chck-type = "a_b">B</p>
<p><input type = "checkbox" chck-type = "c">C</p>
<div class='A_B hide'>
This box is for A or B.
</div>
<div class='C_Box hide'>
This box is for C.
</div>
Просто измените свой JS на это, и все будет работать.
$('input').on('click', function () {
var a_b = $('input[chck-type = "a_b"]').is(':checked');
if (!a_b) {
$('.A_B').addClass('hide');
} else {
$('.A_B').removeClass('hide');
}
var c = $('input[chck-type = "c"]').is(':checked');
if (!c) {
$('.C_Box').addClass('hide');
} else {
$('.C_Box').removeClass('hide');
}
});
.hide {
display: none;
}
<script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p><input type = "checkbox" chck-type = "a_b" />A</p>
<p><input type = "checkbox" chck-type = "a_b" />B</p>
<p><input type = "checkbox" chck-type = "c" />C</p>
<div class = "A_B hide">This box is for A or B.</div>
<div class = "C_Box hide">This box is for C.</div>
Пример: Stackblitz