如何在用户进行选择时切换可见性

How to toggle visibility when user makes a selection

本文关键字:可见性 选择 行选 用户      更新时间:2023-09-26

我有一个带有多个选项的select表单。我还有一个div,我想只在选择特定选项时显示它。你们能给我指对方向吗?最简单的方法是什么?

谢谢:(

这里有一个简单的非jQuery解决方案

window.onload=function() {
  var sel = document.getElementById("sel1");
  sel.onchange=function() {
     document.getElementById("otherDiv").style.display=(this.value=="other")?"":"none";
  }
  sel.onchange(); // set the visibility onload too in case of reload
}
<select id="sel1">
.
.
.
<option value="other">Other</option>
</select>
<div id="otherDiv">Other options</div>

jQuery是一个强大的javascript库,它使这类任务变得微不足道。下面是一个使用它的示例:jQuery-使用单选按钮显示/隐藏div。