jQuery按键事件问题
我有一个函数(filterLeads),我希望在表单字段移开(模糊)时以及在表单字段中按下回车键时调用该函数。
$('#filter input, #filter select').blur(filterLeads);
$('#filter input, #filter select').keypress(function(e){if(e.which == 13) {filterLeads();}});
模糊工作正常,但我在按键事件方面遇到问题。除非在调用 filterLeads() 之前发生某些事情,否则该事件似乎不会被触发;例如:
$('#filter input, #filter select').keypress(function(e){if(e.which == 13) {alert(e.which);filterLeads();}});
在确认警报(显示正确的关键代码)后正确调用filterLeads()。
有什么想法吗? (浏览器FF 3.0)
I have a function (filterLeads) that I wish to call both when a form field is moved away from (blur) and when the enter key is pressed within a form field.
$('#filter input, #filter select').blur(filterLeads);
$('#filter input, #filter select').keypress(function(e){if(e.which == 13) {filterLeads();}});
The blur works correctly but I am having a problem with the keypress event. The event does not appear to be fired unless something comes before the call to filterLeads(); for example:
$('#filter input, #filter select').keypress(function(e){if(e.which == 13) {alert(e.which);filterLeads();}});
This correctly calls filterLeads() after acknowledging the alert (which shows the correct key code).
Any ideas?
(Browser FF 3.0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道为什么你只针对 FF...但你还应该包括 所有非 - FF 浏览器,使用
e.keyCode
。另外,根据 此信息,keypress
可能不是最好使用的事件。尝试这样的事情:
I'm not sure why you are only targeting FF... but you should also include all non-FF browsers by using
e.keyCode
. Alsokeypress
may not be best event to use according to this information.Try something like this:
试一试:您应该从匿名函数中
return false;
,因为否则包含字段的表单(如果有)将被提交并重新加载页面。如果filterLeads需要事件作为参数,请不要忘记编写:
filterLeads(e)
。如果没有其他帮助:Fire
blur()
:(jQuery-fied,因为可能附加了自定义 jQuery 事件)。
Just a try: You should
return false;
from within the anonymous function, because else the form (if there is one) containing the fields gets submitted and the page is reloaded.If filterLeads expects the event as parameter, don't forget to write:
filterLeads(e)
.And if nothing else helps: Fire
blur()
:(jQuery-fied, because there could be custom jQuery events attached).
只需在事件处理程序中添加
e.preventDefault();
即可,如下所示:just add
e.preventDefault();
inside your event handler, like this: