Running a function when enter is pressed in a text box [closed]

与世无争的帅哥 提交于 2019-12-13 11:18:24

问题


This is my input form:

<input type="text" id="word" />

I basically want a Javascript function to execute when the person presses enter in the text box. I'm sorry if this is an obvious question, I couldn't find an existing answer that matched my needs.


回答1:


keyup() is your answer:

$('#word').keyup(function(e) {
    if(e.which == 13) {
        // Do your stuff
    }
});

e.which returns the scan/character code of the pressed key. 13 is for ENTER.

You can find other codes here.




回答2:


$('#word').on('keyup', function(e){
  if(e.which === 13){
    // User pressed enter
  }
});

Testing the event which property will tell you what key the user has pressed.
which is normalized by jQuery, so you won't have any cross-browser issues.



来源:https://stackoverflow.com/questions/13793550/running-a-function-when-enter-is-pressed-in-a-text-box

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