大家好:我目前正在做一个元素,用户将选择一个下拉菜单选项,然后将打开一系列复选框。它一直工作到现在,无论如何都会显示一些复选框,当我选择一个下拉选项时,它不会显示任何内容。到目前为止,这是我的代码:
<select id="select" onchange="test(this)">
<option>hi0</option>
<option>hi1</option>
<option>hi2</option>
<option>hi3</option>
<option>hi4</option>
<option>hi5</option>
<option>hi6</option>
<option>hi7</option>
</select>
<form id="option1">
<input type="checkbox" name="Color" value="School Color">The are a solid school color.
<br>
<input type="checkbox" name="Hem" value="Hem">The are hemmed.
<br>
<input type="checkbox" name="Size" value="Size">The pants are not to big or small.
<br>
<input type="checkbox" name="Style" value="Style">The pants are not sweats or jogging style.
</form>
<form id="option2">
<input type="checkbox" name="Color" value="School Color">The shorts are a solid school color.
<br>
<input type="checkbox" name="Length" value="Length">The shorts are not shorter than your fingertip, or longer than your knees. (Capris okay.)
</form>
<form id="option3">
<input type="checkbox" name="Color" value="School Color">The skirt is a solid school color.
<br>
<input type="checkbox" name"Length" value="Length">The skirt is not shorter than your fingertip.
</form>
<form id="option4">
<input type="checkbox" name="Color" value="School Color">The shirt is a solid school color.
<br>
<input type="checkbox" name="Collar" value="Collar">The shirt has a collar or a turtleneck.
<br>
<input type="checkbox" name="Tuck" value="Tuck">The shirt is long enough to be tucked in.
<br>
<input type="checkbox" name="Logo" value="Logo">The shirt has no logos-Baird logo is accepted.
</form>
<form id="option5">
<input type="checkbox" name="Color" value="School Color">The belt is a solid school color.
<br>
<input type="checkbox" name="Decorations" value="Decorations">The belt has no studs or decorations.
<br>
<input type="checkbox" name="Buckle" value="Buckle">The belt buckle is solid.
<br>
<input type="checkbox" name="Excess" value="Excess">The belt has no excess length (does not hang down).
</form>
<form id="option6">
<input type="checkbox" name="Color" value="School Color">Hair accessories are a solid school color.
</form>
<form id="option7">
<input type="checkbox" name="Color" value="School Color">The shoes and laces are a solid school color.<br>
<input type="checkbox" name="Strap" value="Strap">If the shoes are sandals, they have a back strap.<br>
<input type="checkbox" name="Style" value="Style>The shoes are not high heels, platforms, or steel-toed.
</form>
这是 css 和 javascript:
<head>
<style>
#option1 {
display: none;
}
#option2 {
display: none;
}
#option3 {
display: none;
}
#option4 {
display: none;
}
#option5 {
display: none;
}
#option6 {
display: none;
}
#option7 {
display: none;
}
</style>
<script language="javascript">
// simple <select> / onchange
function test(e) {
var i = e.selectedIndex;
if (i == 1) document.querySelector("#option1").style.display=
"block";
if (i == 2) document.querySelector("#option2").style.display=
"block";
if (i == 3) document.querySelector("#option3").style.display= "block";
if (i == 4) document.querySelector("#option4").style.display=
"block";
if (i == 5) document.querySelector("#option5").style.display=
"block";
if (i == 6) document.querySelector("#option6").style.display=
"block";
if (i == 7) document.querySelector("#option7").style.display=
"block";
}
</script>
</head>
我知道它很多,但是任何人都可以提供建议以防止第一组复选框无论如何都显示,以及为什么它们打不开?感谢您的帮助!
最佳答案
我个人建议:
function test(el){
// gets all form-elements:
var forms = document.querySelectorAll('form');
// iterates over all the form-elements, hiding them
[].forEach.call(forms, function(a){
a.style.display = 'none';
});
// retrieves the form-element with the given 'id', and shows it:
document.querySelector('#option' + (el.selectedIndex)).style.display = 'block';
}
以上确实需要一个相当新的浏览器(但由于您已经在使用 document.querySelector()
,我认为兼容性是给定的),以便使用 document.querySelectorAll()
和 Array.prototype.forEach()
。
引用资料:
关于javascript - 未隐藏复选框选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20900763/