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

javascript - Textarea does not stop making new line when Enter key is pressed - Stack Overflow

programmeradmin6浏览0评论

i don't understand why my textarea won't stop making new lines and won't call function when enter is pressed depsite the fact jquery is to do so. With input it's working ok. And yes i still want that Submit button there.

JavaScript

function da(){

$('#').unbind('keypress').bind('keypress', function(e){
   var code = e.keyCode ? e.keyCode : e.which;
   if(code == 13) // Enter key is pressed
   {  e.preventDefault();
      chat();
   }
});
}

HTML

<form id='' method='post'>


Mesaj:<br>
<textarea name='mesaj' rows='7' col='60'></textarea><br/><br/>
<input type='submit' value='Trimite mesaj!' onclick='chat()'>

</form>"

i don't understand why my textarea won't stop making new lines and won't call function when enter is pressed depsite the fact jquery is to do so. With input it's working ok. And yes i still want that Submit button there.

JavaScript

function da(){

$('#').unbind('keypress').bind('keypress', function(e){
   var code = e.keyCode ? e.keyCode : e.which;
   if(code == 13) // Enter key is pressed
   {  e.preventDefault();
      chat();
   }
});
}

HTML

<form id='' method='post'>


Mesaj:<br>
<textarea name='mesaj' rows='7' col='60'></textarea><br/><br/>
<input type='submit' value='Trimite mesaj!' onclick='chat()'>

</form>"
Share Improve this question edited Nov 21, 2019 at 17:39 Gideon Babu 3562 gold badges5 silver badges11 bronze badges asked Nov 17, 2014 at 14:52 camin10camin10 133 silver badges6 bronze badges 5
  • 1 At least the native keypress is not cancelable, try keydown instead. Notice, that you don't need to try to detect keyCode in jQuery, it's normalized, and is just which in any browser. – Teemu Commented Nov 17, 2014 at 14:55
  • What code is in your chat() function ? – Philip G Commented Nov 17, 2014 at 14:56
  • 1 @Teemu and is just keyCode in any browser... you mean e.which – Roko C. Buljan Commented Nov 17, 2014 at 14:56
  • @RokoC.Buljan Yep, corrected the ment. – Teemu Commented Nov 17, 2014 at 14:59
  • @PhilipG it's irrelevant i can put there an alert to see if it's working. Is a function that send the form that to another php file. – camin10 Commented Nov 17, 2014 at 15:00
Add a ment  | 

3 Answers 3

Reset to default 6

To stop newlines (along with carriage return), you need to capture 10 as well as 13 on keypress using keycode.

See this snippet:

$("textarea").on("keypress", function(e) {
    if ((e.keyCode == 10 || e.keyCode == 13)) {
        e.preventDefault();
        chat();
    }
});

function chat() {
    alert("hello chat");
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name='mesaj' rows='7' col='60'></textarea><br/><br/>
<input type='submit' value='Trimite mesaj!' onclick='chat()' />

You can do this check my code and sample example for your reference

$(".Post_Description_Text").keydown(function(e){
		if (e.keyCode == 13)
		{
		e.preventDefault();
      	}
	});
.Post_Description_Text{ 
    width:400px;
    height:100px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<textarea name="ment_text" id="ment_text"  class="Post_Description_Text" rows="5"></textarea>
	<button id="check_btn">click here to check</button>

Try this:

HTML(add id to your textarea):

<form id='' method='post'>
    Mesaj:<br>
    <textarea name='mesaj' id="mesaj" rows='7' col='60'></textarea><br/><br/>
    <input type='submit' value='Trimite mesaj!' onclick='chat()'/>
</form>

JS(avoid new lines when enter key is pressed):

$('#mesaj').keydown(function(e) {
    if(e.which == 13) {
        e.preventDefault();
        chat();
    }
});

JSFIDDLE: http://jsfiddle/ghorg12110/p3ufLjoe/

发布评论

评论列表(0)

  1. 暂无评论