当在下拉框中选择选项 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";)
- 您需要从那里删除
;
.. - 单个
=
是赋值.. 比较你必须使用==
- 并且由于
selectedIndex
是一个数字,您应该将它与一个数字进行比较,而不是强制进行隐式类型转换。
所以
if (document.getElementById("type").selectedIndex==8)
关于javascript - 尝试根据在下拉框中选择的选项显示/隐藏和元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21535877/