admin管理员组文章数量:1424409
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 badges4 Answers
Reset to default 3heres 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.
本文标签: javascriptMake 39enter39key run a function from an input typequottextquotStack Overflow
版权声明:本文标题:javascript - Make 'enter'-key run a function from an input type="text" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745408768a2657369.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论