本文介绍了当选择选项时,javascript显示隐藏div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我一直在寻找这个答案,但我碰到的一切(即使在搜索JavaScript时)都会提供jQuery!没有人使用普通的javascript了?!???
所以我想要的是,我有一个下拉列表(选择一些选项)。
< select id =testname =form_select >
< option value =0>否< / option>
< option value =1onClickshowDiv()>是< / option>
< / select>
< div id =hidden_divstyle =display:none;> Hello hidden content< / div>
然后我用这个javascript代码试试它:
function showDiv(){
document.getElementById('hidden_div')。style.display =block;
}
我猜我的问题是在我的选项中使用了onClick触发器,但是我我不确定还有什么其他用途?或者我可以完全关闭这个(注意自己得到一本好的JavaScript书!) 解决方案
试试这个: p>
< select id =testname =form_selectonchange =showDiv(this)>
< option value =0>否< / option>
< option value =1>是< / option>
< / select>
函数showDiv(elem){
if(elem.value == 1)
document.getElementById('hidden_div')。style.display =block;
}
Ok I've been searching for answers to this for a while but everything I come across (even when searching for javascript) comes up with jQuery! does nobody use plain javascript anymore?!?
So what I want, I have a drop down list (select with a number of options). 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 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? Or I could be completely off with this one (note to self get a good javascript book!)
解决方案
try this:
<select id="test" name="form_select" onchange="showDiv(this)">
<option value="0">No</option>
<option value ="1">Yes</option>
</select>
function showDiv(elem){
if(elem.value == 1)
document.getElementById('hidden_div').style.display = "block";
}
这篇关于当选择选项时,javascript显示隐藏div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!