如何使用jQuery检测按键盘输入? [英] How to detect pressing Enter on keyboard using jQuery?

查看:135
本文介绍了如何使用jQuery检测按键盘输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测用户是否使用jQuery按 Enter

I would like to detect whether the user has pressed Enter using jQuery.

这怎么可能?是否需要插件?

How is this possible? Does it require a plugin?

编辑:看起来我需要使用 keypress() 方法。

It looks like I need to use the keypress() method.

我想知道是否有人知道该命令是否存在浏览器问题 - 比如是否存在我应该了解的任何浏览器兼容性问题?

I wanted to know if anyone knows if there are browser issues with that command - like are there any browser compatibility issues I should know about?

推荐答案

jQuery的重点在于你不必担心浏览器差异。我很确定你可以安全地使用输入在所有浏览器中都是13。所以考虑到这一点,你可以这样做:

The whole point of jQuery is that you don't have to worry about browser differences. I am pretty sure you can safely go with enter being 13 in all browsers. So with that in mind, you can do this:

$(document).on('keypress',function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

这篇关于如何使用jQuery检测按键盘输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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