英文:
Jquery Checkbox Checked Show / Hide Div if two checkboxes is check or unchecked
问题
以下是翻译好的部分:
#1:如果用户选中复选框"A"或"B",则显示"A_B" div元素。
#2:如果用户取消选中"A"复选框,但仍选中"B"复选框,则"A_B" div元素仍然显示。
#3:如果用户取消选中"A"和"B"复选框,那么只有"A_B" div元素将被隐藏。
现在,当我取消选中"A"或"B"复选框时,div元素将被隐藏,不应该切换。有没有什么改进的方法来实现这3个条件?
任何想法/解决方案都将是很大的帮助。对于遇到与我相同问题的一些用户来说,这也可能会很有用。
英文:
I have this simple jquery code for checkbox that checks via attributes. I know that there are many similarities for this answered already but the idea here is different.
I want to show the A_B
div element for the following condition:
#1: User checked
checkbox A
or B
then shows the A_B
div element
#2: If user unchecked
A
checkbox but checkbox B
is still checked. Then A_B
div element is still showing.
*Note: This can be reversal for the situation*
#3 If user uncheckeds both A
or B
checkbox. Then only A_B
div element will hide.
Right now. When I uncheck A
or B
checkbox. The div element will hide, should not suppose to toggle.
Is there anything here to improve. To achieve the following 3
conditions?
Any ideas / solutions would be a great help. Might be useful also to some users that encounters the same problem as mine. Thanks.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('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');
}
});
<!-- language: lang-css -->
.hide {display:none}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
答案1
得分: 1
您没有检查两个复选框是否都未选中。
$('[chck-type="a_b"]').click(function(){
if ($(this).prop("checked") == true) {
$('.A_B').removeClass('hide');
} else if($('input[chck-type="a_b"]:checked').length === 0) {
$('.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>
英文:
You are not checking if both checkboxes are unchecked or not.
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
$('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');
}
});
<!-- language: lang-css -->
.hide {display:none}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
答案2
得分: 0
$('[chck-type="a_b"]').click(function(){
let checkboxes = $('[chck-type="a_b"]:checked').length;
if (
checkboxes != 0
) {
$('.A_B').removeClass('hide');
} else {
$('.A_B').addClass('hide');
}
});
$('[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>
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$('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');
}
});
<!-- language: lang-css -->
.hide {display:none}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
答案3
得分: 0
只需将您的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');
}
});
您的HTML和CSS部分保持不变。
英文:
Simply change your JS to this and will work.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-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');
}
});
<!-- language: lang-css -->
.hide {
display: none;
}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
Example : Stackblitz
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论