然后我用这个香草 JavaScript 代码尝试它:
函数 showDiv(){document.getElementById('hidden_div').style.display = "block";}
我猜我的问题是我的选项中的 onClick 触发器,但我不确定还可以使用什么?
解决方案
试试这个:
function showDiv(divId, element){document.getElementById(divId).style.display = element.value == 1 ?块":无";}
#hidden_div {显示:无;}
<select id="test" name="form_select" onchange="showDiv('hidden_div', this)"><option value="0">否</option><option value="1">是</option></选择><div id="hidden_div">这是一个隐藏的div</div>
I want to use plain JavaScript. I have a drop down list (<select>
with a number of <option>
s). When a certain option is selected I want a hidden div to display.
<select id="test" name="form_select">
<option value="0">No</option>
<option value ="1" onClick"showDiv()">Yes</option>
</select>
<div id="hidden_div" style="display: none;">Hello hidden content</div>
Then I'm trying it with this vanilla JavaScript code:
function showDiv(){
document.getElementById('hidden_div').style.display = "block";
}
I'm guessing my problem is with the onClick trigger in my options but I'm unsure on what else to use?
解决方案
try this:
function showDiv(divId, element)
{
document.getElementById(divId).style.display = element.value == 1 ? 'block' : 'none';
}
#hidden_div {
display: none;
}
<select id="test" name="form_select" onchange="showDiv('hidden_div', this)">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
<div id="hidden_div">This is a hidden div</div>
这篇关于选择选择选项时如何显示隐藏的 div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!