使用Javascript隐藏元素.不适用于IE和Chrome

Tim*_*imo 5 javascript firefox internet-explorer google-chrome show-hide

我有隐藏元素的问题.我已经阅读过这个主题 Javascript在Firefox上运行但在Chrome和IE6中 却没有,但是从那里得不到帮助.Javascript代码,应该隐藏/显示文本框和单选按钮:

function hide1(a)
{
var text1=document.getElementById("text1");
text1.style.visibility = 'visible';
text1.value = a;

document.getElementById("radio1").style.visibility = 'hidden';
document.getElementById("radio2").style.visibility = 'hidden';
document.getElementById("rlabel").style.visibility = 'hidden';
}

function show1()
{
document.getElementById("text1").style.visibility = 'hidden';
document.getElementById("radio1").style.visibility = 'visible';
document.getElementById("radio2").style.visibility = 'visible';
document.getElementById("rlabel").style.visibility = 'visible';
}
Run Code Online (Sandbox Code Playgroud)

和HTML:

<select id='listb2'>
  <option onclick='hide1("$age");' value='1'>Age</option>
  <option onclick='show1();' value='2'>Sex</option>
</select>
Run Code Online (Sandbox Code Playgroud)

现在使用Firefox和Opera,当我选择其中一个选项时,它会将单选按钮和文本框更改为可见/隐藏.但是对于Chrome和IE,他们不会.我也试过"...... .style.display ='none';" 这一次都没有发生任何事情(在IE和Chrome,FF和Opera上都有效).

FF 5.0; Chrome 12; IE 8; 歌剧11.50

现在适用于:

<select onchange='hideshow(this.value);' id='listb2'>
  <option value='1'>Age</option>
  <option value='2'>Sex</option>
</select>
Run Code Online (Sandbox Code Playgroud)

Tow*_*own 5

onclickoption元素无效.

处理相反的onchange事件,select使用下面的代码获取所选值,然后进行条件处理.

var element = document.getElementById("listb2");
var selectedValue = element.options[element.selectedIndex].value;
Run Code Online (Sandbox Code Playgroud)