选择仅在一个<选项>上的onchange事件框 [英] Select box onchange event with only one <option>

查看:69
本文介绍了选择仅在一个<选项>上的onchange事件框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onchange事件运行良好并且填充我的输入(文本框)就好了,但是当onchange事件应用于下拉框时只有一个选项,它就不起作用了。即使有一个或多个项目,我如何才能获得onchange?

The onchange event works great and populates my input (textbox) just fine, but when the onchange event is applied to the drop down box with only 1 single option in it, it does not work. How can I get the onchange to fire even if, there is one or multiple items?

<!DOCTYPE html>

<html>

<head>

<style type="text/css">

</style>

<script type="text/javascript">
function test(x) {

    var x = document.getElementById(x).options[document.getElementById(x).selectedIndex].text

    document.getElementById('output').value = x

}//end of function
</script>

</head>

<body>
<select id="drop1" onchange="test(this.id)">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
<br><br>
<select id="drop2" onchange="test(this.id)">
  <option value="volvo">Volvo</option>
</select> 
<br><br>
<input type="text" id="output">

</body>

</html>


推荐答案

如果只有一个项目,它永远不会改变。请尝试 onblur 。或者 onclick ,具体取决于实际尝试做什么。

If there is only one item, it never will change. Try onblur instead. Or maybe onclick, depending on what you are actually trying to do.

这篇关于选择仅在一个&lt;选项&gt;上的onchange事件框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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