Javascript 从 textarea 中删除换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4429757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove line break from textarea
提问by user478419
I have a textarea like this:
我有一个这样的文本区域:
<textarea tabindex="1" maxlength='2000' id="area"></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. 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.
问题:发送消息后,textarea没有清除。在第一页加载时,textarea 是空的。提交消息后,textarea 中有一个空行,我不知道如何摆脱它。
回答by Tim Down
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.
问题是 Enter 按键没有被抑制并且正在执行其通常的浏览器行为(即添加换行符)。添加return false
到按键处理程序的末尾以防止出现这种情况。
$("#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;
});
回答by user721277
Thomas, the e.preventDefault(); would need to be wrapped inside a conditional applying it only to the enter key.
托马斯,e.preventDefault(); 需要将其包装在仅将其应用于回车键的条件中。
// 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("");
}
}
} );
回答by Thomas Hunter II
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("");
}
});