如果新值与旧值相同,如何在HTMLSelectElement上触发更改事件? [英] How to fire a change event on a HTMLSelectElement if the new value is the same as the old?

查看:104
本文介绍了如果新值与旧值相同,如何在HTMLSelectElement上触发更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下标记:

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

这篇关于如果新值与旧值相同,如何在HTMLSelectElement上触发更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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