HTML javascript选择动作 [英] HTML javascript select action

查看:79
本文介绍了HTML javascript选择动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个选择列表:

<select>
<option value="0" onclick="anders('1')">Anders</option>
<option value="200" onclick="anders('');" selected="selected">&#8364; 200,-</option>
<option value="300" onclick="anders('')">&#8364; 300,-</option>
<option value="400" onclick="anders('')">&#8364; 400,-</option>
<option value="500" onclick="anders('')">&#8364; 500,-</option>
</select>

当我选择值 0时,需要显示某些内容,这在Firefox中有效,但在IE浏览器。即使具有onclick的警报功能在IE中也不起作用,有人对此有所了解吗?

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?

推荐答案

您真的应该绑定选择本身的onchange事件的逻辑,而不是单个选项的单击事件的逻辑:

You really should probably bind this logic to the onchange event of the select itself, and not the click event of the individual options:

var myDiv = document.getElementById("myDiv");
document.getElementById("mySelect").onchange = function(){
  myDiv.style.display = (this.selectedIndex == 0) ? "block" : "none";
}

通过这种方式绑定时,我们不需要混合HTML和我们的Javascript。我们的HTML看起来很简单,如下所示:

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:

<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>

在线演示: http://jsbin.com/ijogi

这篇关于HTML javascript选择动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆