捕获在页面上任何位置按下的Enter键

Ada*_*dam 82 jquery

我需要随时在登录页面上捕获Enter键.它将启动登录尝试.

使用jQuery,我将如何实现这一目标?我会将它链接到身体标签吗?

sje*_*397 140

$(document).keypress(function(e) {
  if(e.which == 13) {
    // enter pressed
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 我正在尝试这个,我注意到按键没有触发箭头按键,但keydown是! (5认同)

can*_*noe 28

按下某个键时会触发keydown事件.
当按下某个键并且该键通常产生一个字符值时,会触发按键事件.

$(document).keydown( function(event) {
  if (event.which === 13) {
    // login code here
  }
}); 
Run Code Online (Sandbox Code Playgroud)