如何清除输入或换行(html / javascript)的文本框?

时间:2018-02-23 15:30:37

标签: javascript html

我使用HTML,JavaScript和CSS创建了一个简单的在线聊天室。无论如何,我最近添加了一个功能,可以在按下回车键时自动提交消息。但每当我这样做时,文本框的值都有一个新行。

这是一个例子......
在提交消息之前

document.getElementById("send").value;

“” 提交消息后

document.getElementById("send").value;



任何帮助将不胜感激,谢谢:)

1 个答案:

答案 0 :(得分:0)

如果您想完全删除它们

var UserInput = document.getElementById("send").value
UserInput = UserInput.replace(/(?:\r\n|\r|\n)/g, '');

replace函数使用正则表达式检查字符串的返回值和新行,当它找到一个时,它用“”替换它,或者什么都不用。

希望这有助于你