简体   繁体   English

从textarea中删除换行符

[英]Remove line break from textarea

I have a textarea like this: 我有这样的textarea:

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

I watch this textarea with jquery: 我用jquery看这个textarea:

$("#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. 如果按下返回键并且消息不是空白或仅包含行间距,send()会将消息提交给服务器。

The problem: After sending the message, the textarea is not cleared. 问题:发送消息后,textarea不会被清除。 On the first page load, the textarea is empty. 在第一页加载时,textarea为空。 Once a message was submitted, there is one blank line in the textarea and I don't know how to get rid of it. 提交消息后,textarea中有一个空行,我不知道如何摆脱它。

The problem is that the Enter keypress is not being suppressed and is doing its usual browser behaviour (ie adding a line break). 问题是Enter按键没有被抑制,并且正在执行其通常的浏览器行为(即添加换行符)。 Add return false to the end of your keypress handler to prevent this. return false添加到keypress处理程序的末尾以防止这种情况发生。

$("#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;
});

Thomas, the e.preventDefault(); 托马斯,e.preventDefault(); would need to be wrapped inside a conditional applying it only to the enter key. 将需要包含在条件内,仅将其应用于回车键。

// Restrict ENTER.
if (e.keyCode == '13') { e.preventDefault(); }

The whole function would look something like this (with commenting): 整个函数看起来像这样(带注释):

// Key Press Listener Attachment for #area.
$("#area").keypress( function (event) {

    // If the key code is not associated with the ENTER key...
    if (event.keyCode != 13) {

        // ...exit the function.
        return false;

    } else {

        // Otherwise prevent the default event.
        event.preventDefault();

        // Get the message value, stripping any newline characters.
        var msg = $("#area").val().replace("\n", "");

        // If the message is not blank now...
        if (!util.isBlank(msg)) {

            // ...send the message.
            send(msg);

            // Clear the text area.
            $("#area").val("");

        }

    }

} );

You'll want to use the event.preventDefault() function to prevent the default event action from happening, in this case adding the enter character: 您将需要使用event.preventDefault()函数来防止发生默认事件操作,在这种情况下添加输入字符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM