开发者

How to toggle visibility when user makes a selection

开发者 https://www.devze.com 2023-03-18 17:54 出处:网络
I have a select form with several options. I also have a div that I would like to show ONLY when a particular option is selected. Could you guys point me in 开发者_高级运维the right direction? What wo

I have a select form with several options. I also have a div that I would like to show ONLY when a particular option is selected. Could you guys point me in 开发者_高级运维the right direction? What would be the easiest thing to use for this?

Thank you :)


Here is a simple and non-jQuery solution

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 is a powerful javascript library that makes this kind of task trivial. Here is an example of what you're trying to do using it: jQuery - Using the radio button to show/hide a div.

0

精彩评论

暂无评论...
验证码 换一张
取 消