HTML javascript 选择动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2300292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML javascript select action
提问by stefan
I got a selection list:
我得到了一个选择列表:
<select>
<option value="0" onclick="anders('1')">Anders</option>
<option value="200" onclick="anders('');" selected="selected">€ 200,-</option>
<option value="300" onclick="anders('')">€ 300,-</option>
<option value="400" onclick="anders('')">€ 400,-</option>
<option value="500" onclick="anders('')">€ 500,-</option>
</select>
When I select value "0" something need to be visible, this is working in firefox but not in internet explorer. Even an alert function with onclick isn't working in IE, does anybody knows something for this?
当我选择值“0”时,某些内容需要可见,这在 Firefox 中有效,但在 Internet Explorer 中无效。即使带有 onclick 的警报功能在 IE 中也不起作用,有人对此有所了解吗?
回答by Sampson
You really should probably bind this logic to the onchange event of the select itself, and not the click event of the individual options:
您确实应该将此逻辑绑定到选择本身的 onchange 事件,而不是单个选项的单击事件:
var myDiv = document.getElementById("myDiv");
document.getElementById("mySelect").onchange = function(){
myDiv.style.display = (this.selectedIndex == 0) ? "block" : "none";
}
When we bind it this way, we don't need to mix our HTML and our Javascript. Our HTML can look as simple as what follows:
当我们以这种方式绑定它时,我们不需要混合我们的 HTML 和我们的 Javascript。我们的 HTML 看起来很简单,如下所示:
<select id="mySelect" name="values">
<option>0</option>
<option>1</option>
<option>2</option>
</select>
<div id="myDiv">
<p>Select 0 to show me, otherwise I'm invisible!</p>
</div>
Online demo: http://jsbin.com/ijogi
在线演示:http: //jsbin.com/ijogi

