我正在尝试制作一个页面,该页面取决于选中哪个复选框,将显示相应的div /值。如果选中红色复选框,则将显示红色div。如果选中了蓝色收音机,则将显示蓝色div。如果选中了蓝色和绿色,则将显示蓝色和绿色的div,但不显示红色。如果单击全部复选框,所有内容将再次显示,因此我们可以重复此过程,这意味着所有内容都将显示,但是如果我单击蓝色复选框,则仅显示蓝色,其他所有内容都将显示,直到我单击其他值-因此,如果单击蓝色和红色,则将显示蓝色和红色,但不会显示绿色,因为尚未单击绿色。如果未选中蓝色,则仅显示红色
我已经为此工作了几天,我感觉自己已经撞墙了。如果选中了一个复选框,我希望显示该值,那么如果选中了“所有”,“红色”和“绿色”,则将显示“红色”,“绿色”和“蓝色”,因为“所有” '复选框已被选中。如果所有选项均未选中,则蓝色div应该消失,因为未选中它。我一直在使用google,lydna.com和stack来使自己到达目前的位置,但是我无法像我希望的那样使所有方面都能正常工作。
我从链接开始,现在正在使用复选框。如果单击其中一个框,它将显示相应的值。我希望所有带有“ all”类的div在初始页面加载时显示。我已经弄清楚如何显示已检查的内容。
我将.all类设置为最初显示,因为这就是我如何使所有内容在初始加载时显示的方式。我已经撞墙了,我希望对此有所帮助。我浏览了堆栈并阅读了许多示例,但是发现的内容似乎与我尝试执行的操作没有什么比。
仅供参考:这里的学生/新手对javascript有最基本的了解。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Show Hide Elements Using Checkboxes</title>
<!--CSS stylesheet-->
<style>
.box {
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.all{display: block;}
.red {
background: red;
}
.green {
background: green;
}
.blue {
background: blue;
}
label {
margin-right: 15px;
}
</style>
<!--import jQuery cdn-->
<script src=
"https://code.jquery.com/jquery-1.12.4.min.js">
</script>
<script>
// Enable-Disable text input when checkbox is checked or unchecked
// on initial page load, all is checked so everything shows
// if all clicked - show all, if all not checked, remove all that have no other checked values
// jQuery functions to show and hide divisions
$(document).ready(function () {
$('input[type="checkbox"]').click(function () {
var inputValue = $(this).attr("value");
$("." + inputValue).toggle();
});
});
</script>
</head>
<body>
<input type="hidden" name="all" value="false">
<div>
<!--checkboxes-->
<label><input type="checkbox" id="checkbox"
name="colorCheckbox" value="all" checked="true">
all
</label>
<label><input type="checkbox"
name="colorCheckbox" value="red">
red
</label>
<label><input type="checkbox"
name="colorCheckbox" value="green">
green
</label>
<label><input type="checkbox"
name="colorCheckbox" value="blue">
blue
</label>
</div>
<!--Divisions to be shown and hidden-->
<div class="all red box">all Red Selected</div>
<div class="all green box">all Green Selected</div>
<div class="all blue box">all Blue Selected</div>
</body>
</html>
您可以遍历所有复选框,checked
并显示div将其隐藏。
演示代码:
$(document).ready(function() {
$('input[type="checkbox"]').click(function() {
$(".box").hide() //hide all checkboxes
//loop through checked checkboxs
$('input[type="checkbox"]:checked').each(function() {
var inputValue = $(this).attr("value");
$("." + inputValue).show(); //show them
})
});
});
.box {
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.all {
display: block;
}
.red {
background: red;
}
.green {
background: green;
}
.blue {
background: blue;
}
label {
margin-right: 15px;
}
<!--import jQuery cdn-->
<script src="https://code.jquery.com/jquery-1.12.4.min.js">
</script>
<input type="hidden" name="all" value="false">
<div>
<!--checkboxes-->
<label><input type="checkbox" id="checkbox"
name="colorCheckbox" value="all" checked="true">
all
</label>
<label><input type="checkbox"
name="colorCheckbox" value="red">
red
</label>
<label><input type="checkbox"
name="colorCheckbox" value="green">
green
</label>
<label><input type="checkbox"
name="colorCheckbox" value="blue">
blue
</label>
</div>
<!--Divisions to be shown and hidden-->
<div class="all red box">all Red Selected</div>
<div class="all green box">all Green Selected</div>
<div class="all blue box">all Blue Selected</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句