OnClick Dropdown works in FireFox but not Chrome o

2019-09-17 07:46发布

The following code works perfectly in Firefox, but not in IE or Chrome... Any ideas why?

<script type="text/javascript">
function display_div(show){
   document.getElementById('passenger1').style.display = "none";
   document.getElementById('passenger2').style.display = "none";
   document.getElementById('passenger3').style.display = "none";
   document.getElementById(show).style.display = "block";
}
</script>

<select name="#" id="#">
      <option selected="selected"> </option>
      <option onClick="display_div('passenger1');">1</option>
      <option onClick="display_div('passenger2');">2</option>
      <option onClick="display_div('passenger3');">3</option>
</select>

<div id="passenger1" style="display:none;"> hey, 1 works </div>
<div id="passenger2" style="display:none;"> hey, 2 works </div>
<div id="passenger3" style="display:none;"> hey, 3 works </div>

4条回答
贪生不怕死
2楼-- · 2019-09-17 08:12

move the "onClick" from options to the select element

<select onchange="this(this.selectedIndex)">
    <option value="passenger1">1</option>
查看更多
smile是对你的礼貌
3楼-- · 2019-09-17 08:20

Move the onChange function

http://jsfiddle.net/WmEAp/

查看更多
再贱就再见
4楼-- · 2019-09-17 08:28

IE and Chrome don't recognize events on option elements of a select. You can remove the onclick events from your options and add the following onchange event to your select element to get it to work with your current code:

onchange="display_div('passenger' + this.value);"
查看更多
仙女界的扛把子
5楼-- · 2019-09-17 08:35

onclick on option is not a valid click handler. Use onchange on the select instead.

查看更多
登录 后发表回答