简体   繁体   English

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

[英]How can I clear a text box of enter or newline (html / javascript)?

I've created a simple online chat room using HTML, JavaScript and CSS. 我使用HTML,JavaScript和CSS创建了一个简单的在线聊天室。 Anyway, I recently added a feature to automatically submit the message upon the enter key being pressed. 无论如何,我最近添加了一项功能,可以在按下Enter键后自动提交消息。 But whenever I do this, the value of the text box has a new line in it. 但是,每当执行此操作时,文本框的值都会在其中换行。

Here's an example... 这是一个例子
Before submitting a message 提交信息之前

document.getElementById("send").value;

"" “”
After submitting a message 提交信息后

document.getElementById("send").value;

"
"
Any help would be appreciated, thanks :) 任何帮助,将不胜感激,谢谢:)

If you want to completely remove them 如果要完全删除它们

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

The replace function checks the string for returns and new lines using a regular expression, when it finds one, it replaces it with "", or nothing. replace函数使用正则表达式检查字符串是否包含返回值和换行符,当找到一个字符串时,将其替换为“”或什么都不替换。

Hope this helps you 希望这对您有帮助

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

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