从textarea删除换行符 [英] Remove line break from textarea

查看:128
本文介绍了从textarea删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个textarea:



< textarea tabindex =1maxlength ='2000'id =area> ;< / textarea>



我用jquery观看这个textarea:

<$ p (e){
如果(e.keyCode!= 13)返回;
var msg = $(#$> (msg))
{
send(msg); $ b(); val()。replace(\ n,);
if(!util.isBlank $ b $(#area)。val();
}
});

如果按下返回键,send()会将消息提交给服务器,如果消息不是空白或仅包含行空格。



问题:发送邮件后,textarea未被清除。
在第一页加载时,textarea是空的。一旦提交了一条消息,textarea中就有一个空白行,我不知道如何摆脱它。 解决方案

p>问题在于Enter键没有被压制,并且正在执行其通常的浏览器行为(即添加换行符)。将返回false 添加到您的按键处理程序的末尾以防止发生此情况。

  $(#area)。keypress(function(e){
if(e.keyCode!= 13)return;
var msg = $(#area).val()。替换(/ \ / g,);
if(!util.isBlank(msg))
{
send(msg);
$(#area ).val();
}
返回false;
});


I have a textarea like this:

<textarea tabindex="1" maxlength='2000' id="area"></textarea>

I watch this textarea with jquery:

$("#area").keypress(function (e) {
    if (e.keyCode != 13) return;
    var msg = $("#area").val().replace("\n", "");
    if (!util.isBlank(msg))
    {
        send(msg);
        $("#area").val("");
    }
});

send() submits the message to the server if the return key was pressed and if the message is not blank or only containing line spaces.

The problem: After sending the message, the textarea is not cleared. On the first page load, the textarea is empty. Once a message was submitted, there is one blank line in the textarea and I don't know how to get rid of it.

解决方案

The problem is that the Enter keypress is not being suppressed and is doing its usual browser behaviour (i.e. adding a line break). Add return false to the end of your keypress handler to prevent this.

$("#area").keypress(function (e) {
    if (e.keyCode != 13) return;
    var msg = $("#area").val().replace(/\n/g, "");
    if (!util.isBlank(msg))
    {
        send(msg);
        $("#area").val("");
    }
    return false;
});

这篇关于从textarea删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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