Javascript onChange 箭头键
好的,我们都知道 onChange 用于在选项更改时在 select 语句上执行 javascript 代码。但是,如果使用箭头键更改 select 语句,则不会调用 onChange 事件。有办法解决这个问题吗?请帮忙!我知道我是强迫症。
--编辑 1--
刚刚在 IE 中测试了这一点,箭头键确实有效。显然这只是 Chrome。 ** 去检查 Firefox
-- 编辑 2 --
在 Firefox 中测试并在下面的答案谈到更改所需的 onBlur 操作之前意识到。所以这里的答案是:
Internet Explorer 可以识别来自键盘的 onChange 事件并单击它们。 Firefox 和 Chrome 都要求按键事件后跟模糊事件才能调用 onChange。
现在通常情况下,我不喜欢 Internet Explorer,因为它是一堆垃圾……但我想我……不幸的是,不得不说他们是对的。
我对 Chrome 和 Firefox 上模糊事件的原因的理解是为了节省资源,但我不同意这一点。我觉得它应该遵循 onChange 命令的字面解释...叹息...不过,我想我可能是错的。
Ok, so we all know that onChange is used to execute javascript code on a select statement when the option changes. However, if you change a select statement using the arrow keys, the onChange event is not called. Is there a way around this? Please help! I'm OCD I know.
--EDIT 1--
Just tested this in IE and arrow keys do work. Apparently it's just Chrome. ** Goes to check firefox
-- Edit 2 --
Tested in Firefox and realized just before an answer below talked about the onBlur action being required for the change. So the answer here is:
Internet Explorer recognizes onChange events from the keyboard as well as clicking on them.
Firefox and Chrome both require key events to be followed by blur event in order to call onChange.
Now normally, I don't like Internet Explorer, because it's a piece of garbage... But I think I... unfortunately, have to say they got that one right.
My understanding as to the reasoning for the blur event on chrome and firefox is to save resources, but I disagree with that. I feel it should follow the literal interpretation of the command onChange... Sigh... I suppose I'm probably wrong somehow, though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议您在 Key Up 事件中编写所需的代码来捕获按键并检查按键代码。希望这有帮助
I would suggest you to write the required code in Key Up event to capture the Key press and and also check for Key Code. Hope this helps
滚动选择框不被视为更改。当您对选择进行模糊处理并将新选项值应用于选择元素时,就会发生更改。
Scrolling through a select box is not considered a change. The change happens when you blur() the select and the new option value is applied to the select element.
回到这一点,似乎自从提出这个问题以来,Chrome 现在在关键事件后触发 onChange。 Firefox 似乎仍在等待 onblur。 http://jsfiddle.net/2aQBN/
W3C 规范 似乎建议使用
input
事件。但是,在
Chrome 或Firefox 中,选择元素似乎不会触发任何输入事件。 (只需输入元素。)测试演示当前值与最后一个 onchange 值。
http://jsfiddle.net/teynon/MpyHK/5/
Firefox 将在鼠标悬停时更改该值。键的改变也会改变值。但是,onchange 尚未触发。如果在用户打开选择菜单时提交表单,则提交当前突出显示的选项。
来自 W3C:
https://bugzilla.mozilla.org/show_bug.cgi?id 有一个很长的讨论=126379 关于这一点,很多人都要求方向键能够工作。 (还有一些人为 onchange 方法辩护。)
一些用户认为 W3C 在
select
元素的change
事件的规范中完全是错误的。相反,建议我们建议更改规范,以了解我们期望select
的onchange
功能如何工作。仅根据错误报告的数量,当前的功能显然对很多人来说并不直观。 (Mozilla 有 40 个标记为重复项。)
Coming back to this, it appears that since the asking of this question, Chrome now fires onChange after key events. Firefox appears to still wait for onblur. http://jsfiddle.net/2aQBN/
W3C Specification appears to suggest using an
input
event instead.However, no input event appears to fire in
Chrome orFirefox for the select element. (Just input elements.)Test demonstrating the current value vs the last onchange value.
http://jsfiddle.net/teynon/MpyHK/5/
Firefox will change the value onmouseover. The key change will change the value as well. However, the onchange hasn't fired. If the form submits while the user has the select menu open, the currently highlighted option is submitted.
From W3C:
There is a LONG discussion at https://bugzilla.mozilla.org/show_bug.cgi?id=126379 about this with many people asking for the arrow keys to work. (And some defending the onchange approach.)
Some users have suggested that the W3C is flat out wrong in the specification for the
select
element'schange
event. Instead suggesting we propose changes to the specification for how we expect theselect
'sonchange
functionality to work.The current functionality is clearly not intuitive to a large number of people based solely on the number of bug reports. (Mozilla has 40 marked as duplicates.)
这是一个相当肮脏的黑客,但您可以通过执行以下操作强制触发
change
事件:因此您还可以为
change
注册一个事件侦听器,并且当用户通过上面的代码按下您可能只想将其范围限制在 Firefox 上,但据我所知,您必须使用 UA 嗅探来实现这一点,所以这取决于您是否可以接受。
来源< /a>
This is a pretty dirty hack, but you can force the the
change
event to fire by doing this:So you'd register an event listener for
change
as well, and that function would get called when the user presses a key on the<select>
via the code above.You may want to scope this only to Firefox, but AFAIK you'd have to use UA sniffing for that so it's up to you if that's acceptable.
Source
我正在考虑这样的事情(如果值没有改变,则不触发事件):
I'm thinking about something like this (to not trigger event if value wasn't changed):
这是这个请求的实现。为了简洁起见,仅显示代码。请参阅 https://github.com/ida/skriptz /blob/master/js/fields/select/selection_changed.js 以获得评论中的长解释。
Here's a realization of this request. For brevity only showing the code. See https://github.com/ida/skriptz/blob/master/js/fields/select/selection_changed.js for long explanations in comments.