[英]Hide divs based on number of check boxes checked with CSS or JS
I want to hid divs based on number of check boxes checked. 我想根据选中的复选框数量隐藏div。 Div A is visible on page load while div B is hidden.
div A在页面加载时可见,而div B隐藏。 If total number of checkboxes checked is equal 2 or great than 2 than i want to hide div A and show div B.
如果选中的复选框总数等于2或大于2,则我想隐藏div A并显示divB。
<input class="iput" type="checkbox" name="E33" value="33" stock="20"/> A
<input class="iput" type="checkbox" name="E34" value="33" stock="6"/>
<input class="iput" type="checkbox" name="E646" value="33" stock="7"/>
<input class="iput" type="checkbox" name="E46" value="33" stock="7"/>
<input class="iput" type="checkbox" name="E626" value="33" stock="7"/>
<input class="iput" type="checkbox" name="E656" value="33" stock="7"/>
<div class="a"> Hello </div>
<div class="b" style="display:none"> BYE </div>
Total checked boxes= <span id="result"></span>
Have constantly failed to achieve this. 一直未能实现这一目标。 Currently i am able to count the total checked boxes but unable to find a way to use the total count to show and hide divs.
目前,我能够对总复选框进行计数,但无法找到一种使用总计数来显示和隐藏div的方法。
function showChecked(){
var length = document.querySelectorAll("input:checked").length,
text = "Total Number of Items Selected = "
document.getElementById("result").textContent = length;
document.getElementById("final").value = length;
}
document.querySelectorAll("input[type=checkbox]").forEach(i=>{
i.onclick = function(){
showChecked();
}
});
Add a click event listener to each of the checkboxes to increment a counter variable and if the counter is more than or equal to 2, set the display property of the .a
div to none
and the .b
div to block
. 将单击事件侦听器添加到每个复选框以增加计数器变量,如果计数器大于或等于2,则将
.a
div的display属性设置为none
,将.b
div的display属性设置为block
。
You can use a querySelector to get all of your checkboxes if you give them all the same name (like "E33"): 如果您给所有复选框都使用相同的名称(例如“ E33”),则可以使用querySelector来获取所有复选框:
document.querySelectorAll("input[name=E33]");
To get all checked checkboxes with the name "E33", use the :checked
selector: 要获取所有名称为“ E33”的复选框,请使用
:checked
选择器:
document.querySelectorAll("input[name=E33]:checked").length;
<input class="iput" type="checkbox" name="E33" value="33" stock="20"/> A <input class="iput" type="checkbox" name="E33" value="33" stock="6"/> <input class="iput" type="checkbox" name="E33" value="33" stock="7"/> <input class="iput" type="checkbox" name="E33" value="33" stock="7"/> <input class="iput" type="checkbox" name="E33" value="33" stock="7"/> <input class="iput" type="checkbox" name="E33" value="33" stock="7"/> <div class="a" id="a"> Hello </div> <div class="b" style="display:none" id="b"> BYE </div> <script> var a = document.getElementById("a"); var b = document.getElementById('b'); var checked = document.querySelectorAll("input[ name=E33]:checked").length; var checkboxes = document.querySelectorAll("input[name=E33]"); for(let i = 0; i < checkboxes.length; i++){ checkboxes[i].addEventListener("click", function(e){ if(this.checked){ checked++; if(checked>=2){ a.style.display = "none"; b.style.display = "block"; } } else { checked--; if(checked<2){ a.style.display = "block"; b.style.display = "none"; } } }); } </script>
If you want to select your checkboxes by class: 如果要按类别选择复选框:
<input class="input" type="checkbox" name="E33" value="33" stock="20"/> A <input class="input" type="checkbox" name="E34" value="33" stock="6"/> <input class="input" type="checkbox" name="E646" value="33" stock="7"/> <input class="input" type="checkbox" name="E46" value="33" stock="7"/> <input class="input" type="checkbox" name="E626" value="33" stock="7"/> <input class="input" type="checkbox" name="E656" value="33" stock="7"/> <div class="a" id="a"> Hello </div> <div class="b" style="display:none" id="b"> BYE </div> <script> var a = document.getElementById("a"); var b = document.getElementById('b'); var checked = document.querySelectorAll("input.input:checked").length; var checkboxes = document.querySelectorAll("input.input"); for(let i = 0; i < checkboxes.length; i++){ checkboxes[i].addEventListener("click", function(e){ if(this.checked){ checked++; if(checked>=2){ a.style.display = "none"; b.style.display = "block"; } } else { checked--; if(checked<2){ a.style.display = "block"; b.style.display = "none"; } } }); } </script>
A function to scan all of your elements would be the easiest way. 扫描所有元素的功能将是最简单的方法。
In this example, I have created a function named getAmountChecked which scans all checkboxes with the class 'iput'. 在此示例中,我创建了一个名为getAmountChecked的函数,该函数扫描类为'iput'的所有复选框。 This then calculates how many there were, how many are checked and how many aren't checked;
然后,计算出有多少个,被检查了多少个和未被检查了多少个; I like to have access to any important info when I check things like this.
我喜欢在查看此类信息时访问任何重要信息。 This comes in the form of an object with the values totalItems , totalChecked and totalUnchecked .
这以具有值totalItems , totalChecked和totalUnchecked的对象的形式出现 。
My function verifyInputs calls getAmountChecked and uses the value of totalChecked to determine which elements to show/hide. 我的函数调用verifyInputs和getAmountChecked使用totalChecked的值,以确定哪些元素显示/隐藏。
<!DOCTYPE html>
<html>
<head>
<script language="javascript">
function getAmountChecked(className) {
var registeredInputs = document.getElementsByClassName(className);
var totalChecked = 0;
for(var i = 0; i < registeredInputs.length; i++) {
totalChecked+=registeredInputs[i].checked==true;
}
return {totalItems: registeredInputs.length, totalChecked:totalChecked, totalUnchecked: registeredInputs.length-totalChecked};
}
function verifyInputs(className) {
var checkCount = getAmountChecked(className);
document.getElementById('a').style.display = checkCount.totalChecked < 2 ? 'block' : 'none';
document.getElementById('b').style.display = checkCount.totalChecked >= 2 ? 'block' : 'none';
}
</script>
</head>
<body>
<input class="iput" onclick="verifyInputs('iput')" type="checkbox" name="E33" value="33" stock="20"/>
<input class="iput" onclick="verifyInputs('iput')" type="checkbox" name="E34" value="33" stock="6"/>
<input class="iput" onclick="verifyInputs('iput')" type="checkbox" name="E646" value="33" stock="7"/>
<input class="iput" onclick="verifyInputs('iput')" type="checkbox" name="E46" value="33" stock="7"/>
<input class="iput" onclick="verifyInputs('iput')" type="checkbox" name="E626" value="33" stock="7"/>
<input class="iput" onclick="verifyInputs('iput')" type="checkbox" name="E656" value="33" stock="7"/>
<div class="a" id="a"> Hello </div>
<div class="b" id="b" style="display:none"> BYE </div>
</body>
</html>
This is, with caveats, possible entirely in CSS: 注意,这完全可以在CSS中实现:
/* styling the <div> element with a class of "a",
the 'default' state and the state in which
one of the <input> elements are checked: */
div.a,
input:checked~div.a {
display: block;
}
/* styling the <div> element with the class of 'b',
and also the <div> element with the class of 'a'
when it's preceded by two <input> elements that
are checked: */
div.b,
input:checked~input:checked~div.a {
display: none;
}
/* finally we show the <div> with a class of 'b'
when it's preceded by two <input> elements
both of which are checked: */
input:checked~input:checked~div.b {
display: block;
}
div.a, input:checked~div.a { display: block; } div.b, input:checked~input:checked~div.a { display: none; } input:checked~input:checked~div.b { display: block; }
<input class="iput" type="checkbox" name="E33" value="33" stock="20" /> <input class="iput" type="checkbox" name="E34" value="33" stock="6" /> <input class="iput" type="checkbox" name="E646" value="33" stock="7" /> <input class="iput" type="checkbox" name="E46" value="33" stock="7" /> <input class="iput" type="checkbox" name="E626" value="33" stock="7" /> <input class="iput" type="checkbox" name="E656" value="33" stock="7" /> <div class="a"> Hello </div> <div class="b"> BYE </div>
With the CSS above I've used the tilde ( ~
) operator, the general-sibling combinator; 在上面的CSS中,我使用了tilde(
~
)运算符,即同级组合器。 this selects a following sibling element. 这将选择以下同级元素。 The selector:
选择器:
`input:checked ~ input:checked ~ div.b`
will match div.b
if two, or more, <input>
elements are checked. 如果
div.b
两个或更多<input>
元素,则将与div.b
匹配。
This approach works. 这种方法有效。 But, on to the caveats: if there are multiple groups of radio-buttons each group followed by the
<div>
elements, checking <input>
elements in the first group – by virtue of the ~
combinator – will also select, and style, the <div>
elements in the second group: 但是,需要注意的是:如果有多组单选按钮,每组后跟
<div>
元素,则借助~
组合器,检查第一组中的<input>
元素也将选择样式,第二组中的<div>
元素:
div.a, input:checked~div.a { display: block; } div.b, input:checked~input:checked~div.a { display: none; } input:checked~input:checked~div.b { display: block; }
<input class="iput" type="checkbox" name="E33" value="33" stock="20" /> <input class="iput" type="checkbox" name="E34" value="33" stock="6" /> <input class="iput" type="checkbox" name="E646" value="33" stock="7" /> <input class="iput" type="checkbox" name="E46" value="33" stock="7" /> <input class="iput" type="checkbox" name="E626" value="33" stock="7" /> <input class="iput" type="checkbox" name="E656" value="33" stock="7" /> <div class="a"> Hello </div> <div class="b"> BYE </div> <input class="iput" type="checkbox" name="E33" value="33" stock="20" /> <input class="iput" type="checkbox" name="E34" value="33" stock="6" /> <input class="iput" type="checkbox" name="E646" value="33" stock="7" /> <input class="iput" type="checkbox" name="E46" value="33" stock="7" /> <input class="iput" type="checkbox" name="E626" value="33" stock="7" /> <input class="iput" type="checkbox" name="E656" value="33" stock="7" /> <div class="a"> Hello </div> <div class="b"> BYE </div>
To prevent this each of the 'groups' must be wrapped in an element to prevent the general-sibling combinator from matching elements in subsequent groups: 为了防止这种情况,每个“组”必须包装在一个元素中,以防止通用同级组合器匹配后续组中的元素:
div.a, input:checked~div.a { display: block; } div.b, input:checked~input:checked~div.a { display: none; } input:checked~input:checked~div.b { display: block; }
<div> <input class="iput" type="checkbox" name="E33" value="33" stock="20" /> <input class="iput" type="checkbox" name="E34" value="33" stock="6" /> <input class="iput" type="checkbox" name="E646" value="33" stock="7" /> <input class="iput" type="checkbox" name="E46" value="33" stock="7" /> <input class="iput" type="checkbox" name="E626" value="33" stock="7" /> <input class="iput" type="checkbox" name="E656" value="33" stock="7" /> <div class="a"> Hello </div> <div class="b"> BYE </div> </div> <div> <input class="iput" type="checkbox" name="E33" value="33" stock="20" /> <input class="iput" type="checkbox" name="E34" value="33" stock="6" /> <input class="iput" type="checkbox" name="E646" value="33" stock="7" /> <input class="iput" type="checkbox" name="E46" value="33" stock="7" /> <input class="iput" type="checkbox" name="E626" value="33" stock="7" /> <input class="iput" type="checkbox" name="E656" value="33" stock="7" /> <div class="a"> Hello </div> <div class="b"> BYE </div> </div>
Further, a common UI approach for <input>
elements is to wrap them in a <label>
element in order for a click on the text/content of the <label>
to check/uncheck the check-box. 此外,用于
<input>
元素的常见UI方法是将它们包装在<label>
元素中,以便单击<label>
的文本/内容以选中/取消选中该复选框。
If these check-boxes are wrapped in a <label>
then, predictably, the general-sibling combinator will be unable to select the <div>
elements based on the state of the <input>
elements (see: " Is there a CSS parent selector? "). 如果将这些复选框包装在
<label>
那么可以预见的是,同级组合器将无法基于<input>
元素的状态选择<div>
<input>
元素(请参阅:“ CSS父级是否存在?选择器? ”)。 <label>
elements can, of course, appear as siblings (or any other relationship) to the <input>
elements; 当然,
<label>
元素可以显示为<input>
元素的同级(或其他关系)。 although this requires that each <input>
with a <label>
in the document must have an id
property in order for an association to be formed: 尽管这要求文档中每个带有
<label>
<input>
必须具有id
属性才能形成关联:
label { display: inline-block; width: 1.4em; height: 1.4em; text-align: center; line-height: 1.4em; box-shadow: 0 0 0 1px #000; border-radius: 0.2em; cursor: pointer; margin: 0 0.2em; } div.a, input:checked~div.a { display: block; } div.b, input:checked~input:checked~div.a { display: none; } input:checked~input:checked~div.b { display: block; }
<form> <fieldset> <legend>All the labels</legend> <fieldset> <legend>One</legend> <label for="a">a</label> <label for="b">b</label> <label for="c">c</label> <label for="d">d</label> <label for="e">e</label> <label for="f">f</label> </fieldset> <fieldset> <legend>Two</legend> <label for="g">g</label> <label for="h">h</label> <label for="i">i</label> <label for="j">j</label> <label for="k">k</label> <label for="l">l</label> </fieldset> </fieldset> <fieldset> <legend>Group 1</legend> <input class="iput" type="checkbox" name="E33" value="33" stock="20" id="a"> <input class="iput" type="checkbox" name="E34" value="33" stock="6" id="b"> <input class="iput" type="checkbox" name="E646" value="33" stock="7" id="c"> <input class="iput" type="checkbox" name="E46" value="33" stock="7" id="d"> <input class="iput" type="checkbox" name="E626" value="33" stock="7" id="e"> <input class="iput" type="checkbox" name="E656" value="33" stock="7" id="f"> <div class="a"> Hello </div> <div class="b"> BYE </div> </fieldset> <fieldset> <legend>Group 2</legend> <input class="iput" type="checkbox" name="E33" value="33" stock="20" id="g"> <input class="iput" type="checkbox" name="E34" value="33" stock="6" id="h"> <input class="iput" type="checkbox" name="E646" value="33" stock="7" id="i"> <input class="iput" type="checkbox" name="E46" value="33" stock="7" id="j"> <input class="iput" type="checkbox" name="E626" value="33" stock="7" id="k"> <input class="iput" type="checkbox" name="E656" value="33" stock="7" id="l"> <div class="a"> Hello </div> <div class="b"> BYE </div> </fieldset> </form>
More sensibly, and to allow styling of those <label>
elements to reflect the styling of the checked/unchecked nature of the <input>
elements they would follow the elements (although it's perfectly valid to associate one <input>
with multiple <label>
elements, and in this demo I'm leaving the badly-placed original group in place to demonstrate that multiple <label>
elements associated with a single <input>
works): 更明智的做法是,允许这些
<label>
元素的样式反映<input>
元素的选中/未选中性质的样式,它们将跟随这些元素(尽管将一个<input>
与多个<label>
关联起来是完全有效的元素,在此演示中,我将放错位置的原始组保留在原处,以演示与单个<input>
关联的多个<label>
元素有效):
label { display: inline-block; width: 1.4em; height: 1.4em; text-align: center; line-height: 1.4em; box-shadow: 0 0 0 1px #000; border-radius: 0.2em; cursor: pointer; margin: 0 0.2em; } /* styling the <label> adjacent to an <input>: */ input+label { color: #000; background-color: #fff; } /* styling the <label> adjacent to a checked <input>: */ input:checked+label { color: #fff; background-color: limegreen; } div.a, input:checked~div.a { display: block; } div.b, input:checked~input:checked~div.a { display: none; } input:checked~input:checked~div.b { display: block; }
<form> <fieldset> <legend>All the labels</legend> <fieldset> <legend>One</legend> <label for="a">a</label> <label for="b">b</label> <label for="c">c</label> <label for="d">d</label> <label for="e">e</label> <label for="f">f</label> </fieldset> <fieldset> <legend>Two</legend> <label for="g">g</label> <label for="h">h</label> <label for="i">i</label> <label for="j">j</label> <label for="k">k</label> <label for="l">l</label> </fieldset> </fieldset> <fieldset> <legend>Group 1</legend> <input class="iput" type="checkbox" name="E33" value="33" stock="20" id="a"><label for="a">a</label> <input class="iput" type="checkbox" name="E34" value="33" stock="6" id="b"><label for="b">b</label> <input class="iput" type="checkbox" name="E646" value="33" stock="7" id="c"><label for="c">c</label> <input class="iput" type="checkbox" name="E46" value="33" stock="7" id="d"><label for="d">d</label> <input class="iput" type="checkbox" name="E626" value="33" stock="7" id="e"><label for="e">e</label> <input class="iput" type="checkbox" name="E656" value="33" stock="7" id="f"><label for="f">f</label> <div class="a"> Hello </div> <div class="b"> BYE </div> </fieldset> <fieldset> <legend>Group 2</legend> <input class="iput" type="checkbox" name="E33" value="33" stock="20" id="g"><label for="g">g</label> <input class="iput" type="checkbox" name="E34" value="33" stock="6" id="h"><label for="h">h</label> <input class="iput" type="checkbox" name="E646" value="33" stock="7" id="i"><label for="i">i</label> <input class="iput" type="checkbox" name="E46" value="33" stock="7" id="j"><label for="j">j</label> <input class="iput" type="checkbox" name="E626" value="33" stock="7" id="k"><label for="k">k</label> <input class="iput" type="checkbox" name="E656" value="33" stock="7" id="l"><label for="l">l</label> <div class="a"> Hello </div> <div class="b"> BYE </div> </fieldset> </form>
References: 参考文献:
:checked
pseudo-class . :checked
伪类 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.