即使重新选择相同的选项,也会运行选择的更改事件
基本上,我有如下所示的下拉菜单:
<select>
<option>0</option>
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
</select>
我正在尝试编写一个即使您选择相同选项也会触发的函数,即即使打开下拉菜单并重新选择所选选项,我也想要它执行该函数。
Basically, I have drop down menu that looks like this:
<select>
<option>0</option>
<option selected="selected">1</option>
<option>2</option>
<option>3</option>
</select>
I am trying to write a function that is fired even when you select the same option, i.e. even if the drop-down is opened and re-select the selected option, I want it to execute the function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想使用鼠标进行选择,则可以使用
mouseup
。但是,当选择框打开时它也会触发,因此您需要跟踪它被触发的次数(偶数:选择正在打开,奇数:选择正在关闭): ="http://jsfiddle.net/T4yUm/2/">http://jsfiddle.net/T4yUm/2/。If you mean selecting with the mouse, you can use
mouseup
. However, it will fire when the select box is being opened as well, so you'll need to keep track of the amount of times it was fired (even: select is being opened, odd: select is being closed): http://jsfiddle.net/T4yUm/2/.select
并不意味着以这种方式使用 - 在大多数情况下,您可以使用一些技巧来获得这种行为,例如跟踪select
上的鼠标和键盘事件,但不能保证它们会继续工作,或在每个平台上工作。我建议…
select
重置为其默认值,并使用其他一些文本来指示“选择”哪个,或者select
以外的控件。您能更详细地描述一下最终目标吗?
select
isn't meant to be used this way — there are hacks you can use to get this kind of behavior in most cases, like tracking mouse and keyboard events on theselect
, but there’s no guarantee they’ll keep working, or work on every platform.I would suggest either…
select
to its default value on change, and using some other text to indicate which one is “selected”, orselect
.Can you describe the end goal a little more detail?
pimvdb 的答案对我有很大帮助,但我添加了一些额外的位来处理键盘激活和远离选择的导航:
模糊处理程序处理在下拉列表打开时远离选择的导航,keyup 处理程序处理何时您可以使用键盘更改选择的值。其行为是,当用户单击“返回”导航离开时,才被认为最终选择了该值。如果您想要键盘有不同的行为,那应该不难做到。
pimvdb's answer was a big help for me but I added a couple of extra bits to deal with keyboard activation and navigation away from the select:
The blur handler deals with navigating away from the select while the dropdown is open, the keyup handler deals with when you change the select's value with the keyboard. The behaviour is then that the user is only considered to have finally selected the value when they click return to navigate away. If you want a different behaviour with the keyboard that shouldn't be hard to do.
另一种方法是使用
.blur()
事件 - http://jsfiddle.net /VKZb2/4/Pro
无论哪种方式都会触发。
Con
仅当控件失去焦点时才会触发。
An alternative, use the
.blur()
event -- http://jsfiddle.net/VKZb2/4/Pro
This will fire either way.
Con
It only fires once the control loses focus.
这是适用于所有场景的解决方案,这依赖于用户单击选项时发生的 onclick。只需添加先前选择的变量来跟踪新选择是否与先前选择相同
我还写了一篇关于它的文章,这是一个略有不同的主题,但我认为这将有助于解释。
https://medium.com/@ijlal.tanveer294/how-to-know-if-a-select-dropdown-is-open-555a304e7dda
Here is solution that works in all scenarios, this relies on onclick which happens when a user clicks an option. just add a previously selected variable to keep track of if new selection is same as previous or not
also i wrote an article about it, it was a slightly diff topic, but i think it would help explaining.
https://medium.com/@ijlal.tanveer294/how-to-know-if-a-select-dropdown-is-open-555a304e7dda
我遇到了同样的问题。我刚刚向选项标签添加了一个单击事件,并更改了选择的值:
注意:这在 iPhone 或 iPad 设备上不起作用。
I ran into the same issue. I just added a click event to the option tags and change the value of the select:
NOTE: This doesn't work on iPhone or iPad devices.
尝试以下解决方案。它解决了通过鼠标单击或 Esc 键失去焦点的问题。
Try the below solution. It accounts for loss of focus via mouse click or Esc key.