Javascript 使用javascript检测按下的回车键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14251676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:10:41  来源:igfitidea点击:

Detect enter key pressed using javascript

javascriptenter

提问by Matt

Possible Duplicate:
Javascript capture key

可能重复:
Javascript 捕获密钥

I'm working on reply function using mysql and php.

我正在使用 mysql 和 php 处理回复功能。

My MySQL db:

我的 MySQL 数据库:

reply:
  rid - id;
  topicid - under which topic;
  uid - id of the guy who replies;
  content - what did the guy said.

Here's my html code:

这是我的 html 代码:

<form id="replyform">
  <textarea name="rplcont" placeholder="Press enter to submit your reply."><textarea>
</form>

My question is: How do I know if enter button is pressed?

我的问题是:我怎么知道是否按下了输入按钮?

Many thanks.

非常感谢。

/----------------------------------------------/

/ ----------------------------------------------/

here's my codes that work:

这是我的工作代码:

html:

html:

<textarea id="reply" name="reply" onKeyPress="enterpressalert(event, this)"></textarea>

js:

js:

function enterpressalert(e, textarea){
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
 alert('enter press');
}
}

and it works.

它有效。

Thanks for everyone who concerns this topic!

感谢所有关心这个话题的人!

回答by Arun Killu

var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
    alert('enter press');
}