最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

internet explorer - Hide element with Javascript. Doesn't work with IE and Chrome - Stack Overflow

programmeradmin2浏览0评论

I have problems with hiding elements. I already read this topic Javascript working on Firefox but not in Chrome and IE6 but didn't get help from there. Javascript code, what should hide/show textbox and radio buttons:

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';
}

And HTML:

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

Now with Firefox and Opera it change radio buttons and textbox to visible/hidden when I choose one of the options. But with Chrome and IE they wouldn't. I also tried with "... .style.display = 'none';" nothing happened (on IE and Chrome, FF and Opera it works) even this time.

FF 5.0; Chrome 12; IE 8; Opera 11.50

Now works with:

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

I have problems with hiding elements. I already read this topic Javascript working on Firefox but not in Chrome and IE6 but didn't get help from there. Javascript code, what should hide/show textbox and radio buttons:

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';
}

And HTML:

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

Now with Firefox and Opera it change radio buttons and textbox to visible/hidden when I choose one of the options. But with Chrome and IE they wouldn't. I also tried with "... .style.display = 'none';" nothing happened (on IE and Chrome, FF and Opera it works) even this time.

FF 5.0; Chrome 12; IE 8; Opera 11.50

Now works with:

<select onchange='hideshow(this.value);' id='listb2'>
  <option value='1'>Age</option>
  <option value='2'>Sex</option>
</select>
Share Improve this question edited May 23, 2017 at 12:07 CommunityBot 11 silver badge asked Jul 20, 2011 at 12:35 TimoTimo 1532 gold badges3 silver badges7 bronze badges 2
  • 2 Does a <option onclick> fire at all? Try alerting something inside hide1. – pimvdb Commented Jul 20, 2011 at 12:36
  • No, it doesn't. Is there anything I can do? – Timo Commented Jul 20, 2011 at 12:50
Add a ment  | 

4 Answers 4

Reset to default 5

onclick isn't valid for option elements.

Handle the onchange event of the select instead, get the selected value using the code below and then do your conditional processing.

var element = document.getElementById("listb2");
var selectedValue = element.options[element.selectedIndex].value;

Instead of setting 'visibility' to 'hidden', try setting 'display' to 'none'.

document.getElementById('header').style.display = 'none';

EDIT: Not quite sure whether you can set the onclick property on an option element in those browsers... Try using onchange on your surrounding select tag instead.

I realy encourage you to use jQuery for that. jQuery guarantees that this will work in every browser. In query its very easy

<script>
    $().ready(function () {
        $("#listb2").change(function () {
            if ($("#listb2").val() == "1")
                $("#radio1, #radio2, #rlabel").show();
            else
                $("#text1, #radio1, #radio2, #rlabel").hide();
        });
    });
</script>

<select id='listb2'>
  <option value='1'>Age</option>
  <option value='2'>Sex</option>
</select>

If you remove the onclick events from the select box like this:

<select id='listb2'>
  <option value='1'>Age</option>
  <option value='2'>Sex</option>
</select>

Then you can bind an onChange event using javascript:

var selectBox = document.getElementById("listb2");
selectBox.onchange = function(e){
    if(selectBox.value == 1) {
        // Do what you want for Age
        hide1();
    } else if(selectBox.value == 2) {
        // Do what you want for Sex
        show1();
    }
}
发布评论

评论列表(0)

  1. 暂无评论