event.originalEvent 在具有 jQuery 自动完成功能的焦点事件中未定义
我需要使用自定义操作覆盖自动完成焦点事件,并根据用户是否使用键盘而不是鼠标从列表中进行选择来执行某些操作。为此,我检查事件对象中的originalEvent.type,该对象应该包含执行的操作类型(keydown、keyup、mouseenter 等)。
但是,originalEvent 对象似乎未定义,我不明白为什么。它在自动完成代码中的实际焦点事件中工作得很好,但当我覆盖自动完成对象内部的该事件时则不然。
请参阅下面我的代码。我很感激任何帮助,因为它让我发疯。
$( "#tags" ).autocomplete({
source: availableTags,
focus: function(event, ui) {
//Check whether focus was triggered by a mouse or keyboard event
if ( /^key/.test(event.originalEvent.type) ) {
//Do something here
}
return false;
}
});
I need to override the autocomplete focus event with a custom action and do something based on whether the user was selecting from the list using the keyboard and not the mouse. To do that I'm checking the originalEvent.type in the event object which is supposed to contain the type of action performed (keydown, keyup, mouseenter, etc).
However, the originalEvent object seems to be undefined, and I can't understand why. It is working just fine in the actual focus event in the autocomplete code, but not when I override that event inside of the autocomplete object.
Please see my code below. I'd appreciate any help on this as it's driving me bananas.
$( "#tags" ).autocomplete({
source: availableTags,
focus: function(event, ui) {
//Check whether focus was triggered by a mouse or keyboard event
if ( /^key/.test(event.originalEvent.type) ) {
//Do something here
}
return false;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实际上能够到达
originalEvent
,但我认为即使您可以检索它也对您没有帮助;查看以下示例中的控制台: http://jsfiddle.net/andrewwhitaker/DkumP/1/< /a>您会注意到
originalEvent
的type
是menufocus
,我认为这不是您所期望的。完成您想要做的事情的一种可能方法是检查
event.which
,并查看它是向上箭头还是向下箭头(假设这些是您可以在自动完成小部件中使用的唯一键) ):这是一个示例: http://jsfiddle.net/andrewwhitaker/DkumP/2/
I was actually able to get to
originalEvent
, but I don't think it helps you even if you can retrieve it; look at the console in the following example: http://jsfiddle.net/andrewwhitaker/DkumP/1/You'll notice that the
type
oforiginalEvent
ismenufocus
, which I don't think is what you were expecting.A possible way to accomplish what you're trying to do is examine
event.which
, and see if it is the up arrow or down arrow (assuming those are the only keys you can use in the autocomplete widget):Here's an example: http://jsfiddle.net/andrewwhitaker/DkumP/2/
谢谢你的建议,安德鲁。
我忘了提及我正在使用 jQuery UI 1.8.5,因此检查“which”属性对我来说也不起作用,因为它也未定义。升级到 1.8.10 有所帮助,它开始工作。显然,这整件事是 1.8.5 中的一个错误,后来被修复。
因此,如果其他人遇到类似的问题 - 升级到最新版本,应该可以解决它。
Thanks for the suggestion, Andrew.
I forgot to mention that I was using jQuery UI 1.8.5, so checking the "which" property didn't work for me either since it was undefined as well. Upgrading to 1.8.10 helped tho and it started working. Apparently, this whole thing was a bug in 1.8.5 which was fixed later on.
So in case anyone else is struggling with a similar issue - upgrade to the latest version, that should resolve it.