admin管理员组文章数量:1399260
I have a textarea field and it's data will be submitted using java script and ajax by pressing enter key, the problem is that when the user press the enter key first it will create a new line then the textarea will be submitted, and i don't want it to create a new line.
Please help!
I have a textarea field and it's data will be submitted using java script and ajax by pressing enter key, the problem is that when the user press the enter key first it will create a new line then the textarea will be submitted, and i don't want it to create a new line.
Please help!
Share Improve this question asked Jun 27, 2015 at 4:25 Mohammad Samim SamimeeMohammad Samim Samimee 572 silver badges8 bronze badges 1- 1 With jquery, bind the keyup event of the textarea, capture the event object in the first argument. Use this object to cancel event when the pressed key is enter. I believe it s code is 13. jQuery has all the doc for that, SO all the posts for that also. One thing not to do is to cancel the propagation, otherwise you may need to also trigger submit event at that point. – user4466350 Commented Jun 27, 2015 at 4:28
2 Answers
Reset to default 8Just listen to the keydown
event and prevent the default action (which is the linebreak)
$("textarea").on("keydown", function(e) {
if(e.which === 13) { // enter key
e.preventDefault(); // prevents linebreak
// here you could add your submit call
return false;
}
});
You can use jQuery to determine whether the textarea is active:
$("#foo").is(":focus") // expression is true only when it is active
So your code should look something like this:
if (<enter key pressed> and !($("#foo").is(":focus")))
<submit with JavaScript and Ajax>
本文标签: javascriptMake the Enter key to not create a line breakStack Overflow
版权声明:本文标题:javascript - Make the Enter key to not create a line break - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744193593a2594631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论