HTML SELECT - 即使未更改选项,也会触发JavaScript ONCHANGE事件 [英] HTML SELECT - Trigger JavaScript ONCHANGE event even when the option is not changed

查看:267
本文介绍了HTML SELECT - 即使未更改选项,也会触发JavaScript ONCHANGE事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下标记:

<select onchange="jsFunction()">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

当用户拉下组合框并选择之前选择的相同选项时(或不是完全改变选择),JavaScript不认为它是 onchange 事件。因此,不会调用 jsFunction()。但是我希望在这种情况下调用 jsFunction()。我怎样才能做到这一点?

When a user pulls down the combobox and selects the same option that was previously selected (or doesn't change the selection at all), JavaScript doesn't regard it as an onchange event. So, the jsFunction() is not called. But I want the jsFunction() called even in this case. How can I achieve this?

推荐答案

我这样做:

<select onchange="jsFunction()">
  <option value="" disabled selected style="display:none;">Label</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

如果您需要,您可以拥有与第一个选项相同的标签,在本例中为1。
甚至更好:在框中放置一个标签以供选择。

If you want you could have the same label as the first option, which in this case is 1. Even better: put a label in there for the choices in the box.

这篇关于HTML SELECT - 即使未更改选项,也会触发JavaScript ONCHANGE事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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