在textarea中按Enter键提交值,按Shift + Enter键应转到下一行 [英] Submit value on pressing Enter in textarea and pressing Shift+Enter should go to next line

查看:275
本文介绍了在textarea中按Enter键提交值,按Shift + Enter键应转到下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个聊天框(textarea),如果用户按Enter键,则应提交聊天,如果用户按Shift + Enter,则应输入新行。

I want to have a chat box (textarea) where if user press Enter then it should submit the chat, and if user press Shift+Enter then it should enter in a new line.

我尝试了一些但却无法弄清楚确切的keyup或keydown事情。我目前使用的代码是:

I tried something but not able to figure out the exact keyup or keydown thing. The code I'm using at the moment is:

$("textarea").keydown(function(e){
    if (e.keyCode == 13 && !e.shiftKey)
    {
        e.preventDefault();
    }
}); 

jsFiddle

当我按下Enter + Shift键时,我想让 \ n 到位。

Also I want to get the \n in place when Enter+Shift key is pressed.

编辑

我的代码问题是: -

Issue with my code is:-

当我使用alert检查客户端上的内容时,它会显示下一行。但是当我发布它时,我的轨道会结束。然后它只是一个简单的字符串。没有新线路。

When i am check the content on client using using alert then it shows the next line. But when i am posting it my rails back end. Then its just a simple string. No new line thing is there.

这就是我向rails服务器发送聊天的方式。

This is how i am sending chats to rails server.

  $.post("/incomingchat", { body:$("#chat_" + group_id).val() },
   function(data){
        // do something..
    });


推荐答案

我之前已经回答了这个问题。如果插入符号不在textarea的末尾,这有点棘手:

I've answered this before. It's a little tricky if the caret is not at the end of the textarea:

如何检测shift + enter"并在Textarea中生成一个新行?

这篇关于在textarea中按Enter键提交值,按Shift + Enter键应转到下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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