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

时间:2022-04-19 20:11:13

I need to capture an Enter Key press at any time anywhere on a logon page. It will initiate a logon attempt.

我需要在登录页面的任何位置捕获一个Enter Key press。它将启动登录尝试。

Using jQuery, how would I accomplish this? And would I link it to the body tag?

使用jQuery,我如何实现这一点?我可以把它链接到body标签上吗?

2 个解决方案

#1


128  

$(document).keypress(function(e) {
  if(e.which == 13) {
    // enter pressed
  }
});

#2


16  

The keydown event is fired when a key is pressed down.
The keypress event is fired when a key is pressed down and that key normally produces a character value.

当按下键时,将触发keydown事件。当按下一个键并该键通常产生一个字符值时,将触发keypress事件。

$(document).keydown( function(event) {
  if (event.which === 13) {
    // login code here
  }
}); 

#1


128  

$(document).keypress(function(e) {
  if(e.which == 13) {
    // enter pressed
  }
});

#2


16  

The keydown event is fired when a key is pressed down.
The keypress event is fired when a key is pressed down and that key normally produces a character value.

当按下键时,将触发keydown事件。当按下一个键并该键通常产生一个字符值时,将触发keypress事件。

$(document).keydown( function(event) {
  if (event.which === 13) {
    // login code here
  }
});