按下Enter键后,Textarea不会停止换行 [英] Textarea won't stop making new line when Enter is pressed

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

问题描述

我不明白为什么我的textarea不会停止创建新行,并且在按下输入时不会调用函数。depsite jquery会这样做。随着输入它工作正常。

JavaScript

 函数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>


解决方案

要停止换行(以及回车),您需要捕获 10 以及 13 on keypress 使用键码



看到这个片段:

(keypress,function(e))。()();(class) {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!'onc lick ='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天全站免登陆