选择选项和div visibilty属性 [英] Select options and div visibilty attribute
问题描述
你好!
如果选择列表中的一个选项是,我试图将div的可见性属性设置为隐藏选择。但是,我无法让它工作并怀疑它只是代码中的一个小错误。
< 选择 名称 = selectName id = selectID < span class =code-attribute> onchange = functionName() >
< 选项 value = > 空值< / option >
< 选项 value = op1 > option1 < / option >
< 选项 value = op2 > option2 < / option >
< / select >
< div id = div1 style = 公开程度:可见 >
< 输入 type = text name = name1 id = ID / >
< / div >
< div id = div2 style = 可见性:隐藏 >
< 输入 type = text name = name2 id = ID2 / >
< / div >
以上是我目前拥有的HTML代码,以下是Javascript功能。
function functionName()
{
var a = document .getElementById(' DIV1' 跨度>);
var b = document .getElementById(' div2');
if ( document .getElementById(' selectID')。value = op2 跨度>);
{
a.style.visibility = hidden;
b.style.visibility = visible;
}
else
{
a.style.visibility = visible;
b.style.visibility = hidden;
}
}
此代码的含义是:
取决于选择了哪两个选项,将显示用户用于输入数据的相关div。所以如果选择了选项2,div2将可见,div1将被隐藏。当选项改变时,div'也应该交换。
我在互联网上寻找指南/一些提示,但我认为它会是在这里更好地讨论,当问题(如果有的话)得到解决时,它将对其他人有所帮助。
我能看到的明显错误是:你的if语句应与==(比较)运算符一起使用,而不是与=(赋值)一起使用。
-SA
Hi there!
I''m trying to make a div''s visibility attribute be set to hidden if one of the options in a select list is selected. However, I’m having trouble getting it to work and suspect its just a small mistake in the code.
<select name="selectName" id="selectID" onchange="functionName()">
<option value="">Empty value</option>
<option value="op1">option1</option>
<option value="op2">option2</option>
</select>
<div id="div1" style="visibility:visible">
<input type="text" name="name1" id="ID" />
</div>
<div id="div2" style="visibility:hidden">
<input type="text" name="name2" id="ID2" />
</div>
Above is the HTML code I currently have, and below is the Javascript function.
function functionName()
{
var a = document.getElementById('div1');
var b = document.getElementById('div2');
if (document.getElementById('selectID').value = "op2");
{
a.style.visibility = "hidden";
b.style.visibility = "visible";
}
else
{
a.style.visibility = "visible";
b.style.visibility = "hidden";
}
}
The idea of this code is:
Depending on which of the 2 options is selected, the relevant div will be visible which the user uses to input data. so if option 2 is selected, div2 will be visible and div1 will be hidden. When the option changes the div''s should swap too.
I''ve looked around on the internet for a guide/some tips but I think it would be better discussed on here, and when the problem(if there is one) is resolved it will be helpful to others.
The apparent bug I can see is: your "if" statement should be used with "==" (comparison) operator, not with "=" (assignment).
—SA
这篇关于选择选项和div visibilty属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!