javascript - 尝试根据在下拉框中选择的选项显示/隐藏和元素

标签 javascript html css

当在下拉框中选择选项 value='8' 时,我想显示 div id='other'。如果选择了任何其他值,我还希望它再次隐藏。 这是我到目前为止想出的:

<script>
function other()
{
if (document.getElementById("type").selectedIndex="8";)
{
document.getElementById("other").style.display="block";
}
else
{
document.getElementById("other").style.display="none";
}
}
</script>
<select id='type' onchange='other()'>
<option value="0">Logo</option>
<option value="1">Banner</option>
<option value="2">Static Advert</option>
<option value="3">Forum Signature</option>
<option value="4">YouTube Banner</option>
<option value="5">YouTube Icon</option>
<option value="6">Facebook Cover</option>
<option value="7">Twitter Background</option>
<option value="8">Other (please specify)</option>
</select>
<br/>
<div style='display:none;' id='other'>
If other, specify what you would like.
<input type='text' class='text' name='type' value size='10'/>
</div>

最佳答案

你在一行中有两个错误

if (document.getElementById("type").selectedIndex="8";)
  1. 您需要从那里删除 ;..
  2. 单个 = 是赋值.. 比较你必须使用 ==
  3. 并且由于 selectedIndex 是一个数字,您应该将它与一个数字进行比较,而不是强制进行隐式类型转换。

所以

if (document.getElementById("type").selectedIndex==8)

工作演示在 http://jsfiddle.net/gaby/W7qTV/

关于javascript - 尝试根据在下拉框中选择的选项显示/隐藏和元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21535877/

相关文章:

html - 使用 sprite 时在 Joomla 中隐藏菜单项

javascript - jQuery Mobile 在页面转换之前阻止滚动到顶部?

css - JSP 上下文路径

隐藏在 Chrome 和 Safari 中但不在 Firefox 中的 HTML5 视频控件

javascript - 从 Node 到浏览器,提供和播放 wav 音频文件的不同方式的优缺点

javascript - 卡路里计算器 JavaScript 表单

c# - 我应该如何从 Controller 操作 c# asp.net-mvc-2 返回图像?

html - &lt;!-- [if IE]> 应用边距不应用样式

javascript - Angular 库中提供的 HttpInterceptor 不适用于 Angular 应用程序

javascript - 通过javascript函数使多个具有相同类的div同时工作