Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

javascript - jQuery Event Keypress:按下了哪个键?(jQuery Event Keypress: Which key was pressed?)

With jQuery, how do I find out which key was pressed when I bind to the keypress event?

(使用jQuery,如何确定绑定到keypress事件时按下了哪个键?)

$('#searchbox input').bind('keypress', function(e) {});

I want to trigger a submit when ENTER is pressed.

(我想在按ENTER键时触发提交。)

[Update]

([更新])

Even though I found the (or better: one) answer myself, there seems to be some room for variation ;)

(即使我找到了(或者更好:一个)自己回答,似乎还有一些变化的空间;))

Is there a difference between keyCode and which - especially if I'm just looking for ENTER , which will never be a unicode key?

(有没有之间的差异keyCodewhich -尤其是如果我只是在寻找ENTER,这将永远是一个unicode关键?)

Do some browsers provide one property and others provide the other one?

(有些浏览器提供一种属性,而其他浏览器提供另一种属性吗?)

  ask by BlaM translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually this is better:

(其实这更好:)

 var code = e.keyCode || e.which;
 if(code == 13) { //Enter keycode
   //Do something
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...