最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make 'enter'-key run a function from an input type="text" - Stack Overflow

programmeradmin3浏览0评论

So, I'm having a small problem here.

I have a textfield (not area) and I want a JS function to be run when I press enter. This is instead of having a separate 'submit' button. Right now I'm trying to use the 'onKeyPress' event, but that runs the function every time I type one letter in. So if I want to write 'LIKE', it posts 'L' then 'LI' then 'LIK' and then 'LIKE'. It reacts every time I press anything, not only when I enter.

I do not have a form, just a simple input type="text":

<input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>

If some of you remember my last question, this will be a part of my facebook_footer variable (I'm trying out the FB API), and the finished thing looks like this:

var facebook_footer = '<button onClick="fbLike(\''+post_id+'\')"> Like </button> <input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>';

Do any of you know what I can do to make this work?

So, I'm having a small problem here.

I have a textfield (not area) and I want a JS function to be run when I press enter. This is instead of having a separate 'submit' button. Right now I'm trying to use the 'onKeyPress' event, but that runs the function every time I type one letter in. So if I want to write 'LIKE', it posts 'L' then 'LI' then 'LIK' and then 'LIKE'. It reacts every time I press anything, not only when I enter.

I do not have a form, just a simple input type="text":

<input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>

If some of you remember my last question, this will be a part of my facebook_footer variable (I'm trying out the FB API), and the finished thing looks like this:

var facebook_footer = '<button onClick="fbLike(\''+post_id+'\')"> Like </button> <input type="text" id="' + post_id + '" onKeyPress="fbComment(\''+post_id+'\')"> Post a ment </input>';

Do any of you know what I can do to make this work?

Share Improve this question edited Mar 15, 2013 at 21:43 Igy 43.8k8 gold badges92 silver badges114 bronze badges asked Mar 15, 2013 at 20:35 AleksanderAleksander 2,8155 gold badges38 silver badges59 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

heres a simple function -

textElement.onkeydown=function(e){
    if(e.keyCode==13){
        alert('you press enter')
    }
}

You can detect the enter key within the input:

$("#inputIDHERE").keypress(function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

Sure, you can do that - you'll just need some extra logic in your javascript.

Check out Keyboard Events Tutorial

You just have to peek into the keypress event to see what key was entered. The event object has a keycode or which property that will have a value to correspond to the key pressed, and keycode 13 is the enter/return button.

Additional reference: Javascript Character Codes

Here's what worked for me:

$("#inputIDHERE").bind('keypress', function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

I also added <form></form> around my input tag, though that may not be necessary.

发布评论

评论列表(0)

  1. 暂无评论