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

Javascript 尝试根据下拉框中选择的选项显示/隐藏和元素,javascript,html,css,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.getElemen

我想在下拉框中选择选项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”)
{
document.getElementById(“其他”).style.display=“块”;
}
其他的
{
document.getElementById(“其他”).style.display=“无”;
}
}
标志
横幅
静态广告
论坛签名
YouTube横幅
YouTube图标
Facebook封面
推特背景
其他(请具体说明)

如果其他,请指定您想要的内容。
行中有两个错误

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


    进行演示时,您应该解释您的帖子中出现了什么问题,并给出代码。您没有提供关于哪些代码不起作用的任何信息。只是对一点进行了评论:当您编写if(=“8”)时,您的意思是if(=“8”)吗?我还强烈建议使用jQuery删除大量额外的代码。@ThatiditGuy,重点是我不知道哪些代码不起作用,因此问题是错误是什么?你能查一下控制台日志吗?编辑:用JSFIDLE检查下面Gaby的答案
    if (document.getElementById("type").selectedIndex==8)