按Enter键时,Textarea不会停止换行 [英] Textarea does not stop making new line when Enter key is pressed

查看:67
本文介绍了按Enter键时,Textarea不会停止换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么当按下Enter键时,我的textarea不会停止创建新行并且不会调用函数,而jquery确实这样做了.输入后一切正常.是的,我仍然希望在那里有提交"按钮.

JavaScript

function da(){

$('#com').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='com' method='post'>


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

</form>"

解决方案

要停止换行符(以及回车符),您需要使用keycodekeypress上捕获1013./p>

查看以下代码段:

 $("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.com/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()' /> 

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(){

$('#com').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='com' method='post'>


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

</form>"

解决方案

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.com/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()' />

这篇关于按Enter键时,Textarea不会停止换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆