我有一个选择框,当您单击一个选项时,希望在隐藏的 div 中显示帮助文本:
<select onchange="optionCheck()" multiple="multiple" name="test" id="options" size="15" class="form-select">
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<div id="help1" style=visibility:hidden>help text 1</div>
<div id="help2" style=visibility:hidden>help text 2</div>
<script type="text/javascript">
function optionCheck(){
var option = document.getElementById("options").value;
if(option == "1"){
document.getElementById("help1").style.visibility ="visible";
document.getElementById("help2").style.visibility ="hidden";
}
if(option == "2"){
document.getElementById("help2").style.visibility ="visible";
document.getElementById("help1").style.visibility ="hidden";
}
}
</script>
如何创建一个简单的循环,这样我就不必为每个选项编写“if”语句?
document.getElementById(option).style.visibility ="visible";
不工作 - javascript 和我相处得不好 ;-)
此外,还有一种简单的方法来隐藏选项时的所有其他div吗?