Detect enter key pressed using javascript [duplicate]

让人想犯罪 __ 提交于 2019-11-27 20:43:34

问题


Possible Duplicate:
Javascript capture key

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

My MySQL db:

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:

<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:

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

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!


回答1:


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


来源:https://stackoverflow.com/questions/14251676/detect-enter-key-pressed-using-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!