javascript 文本框回车(textarea)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7078783/
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
Carriage returns for textbox (textarea)
提问by Elistan
I have a textarea with attribute "wrap"="hard" (actually it's server side textbox, but with multiple text mode).
我有一个属性为“wrap”=“hard”的文本区域(实际上它是服务器端文本框,但具有多个文本模式)。
<asp:TextBox TextMode=MultiLine runat=server ID=txt Width=50 Height=50 class=txtclass />
<asp:Button runat=server ID=btnServer OnClick=btn_Click Width=80 Text="Click server" />
<input type="button" value="Click client" onclick="clientclick();" id="btnClient" style="width: 80px;" />
protected void Page_Load(object sender, EventArgs e)
{
txt.Attributes.Add("wrap", "hard");
}
I enter a text that is wider than the textarea. When I click on client side button the text in alert is without carriage returns (like "111111111").
我输入的文本比 textarea 宽。当我单击客户端按钮时,警报中的文本没有回车(如“111111111”)。
<script src="jquery-1.5.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function clientclick() {
alert($('.txtclass').val());
}
When I click on server button while debugging I see that the text has carriage returns (like "11111\r\n1111").
当我在调试时单击服务器按钮时,我看到文本有回车符(如“11111\r\n1111”)。
protected void btn_Click(object sender, EventArgs args)
{
var test = txt.Text;
}
The question is how can I get a text with carriage returns on the client side?
问题是如何在客户端获取带有回车的文本?
采纳答案by tonycoupland
There are a few people who have posted the same question as you before, and general consensus is that there is no simple, out of the box solution. The fix seems to be to walk the string one word at a time and add the newlines in when it would wrap.
有几个人和你之前发布过同样的问题,普遍的共识是没有简单的、开箱即用的解决方案。解决方法似乎是一次一个单词地遍历字符串,并在它换行时添加换行符。
Some sample JS is provided in this case - finding "line-breaks" in textarea that is word-wrapping ARABIC text
在这种情况下提供了一些示例 JS -在自动换行阿拉伯文本的 textarea 中找到“换行符”
回答by Mark
Since you are dealing with HTML, you can use the '< BR >' tag to force a line break inside a text box. I struggled with this for a while and finally found this solution. It worked perfectly for me.
由于您正在处理 HTML,因此您可以使用“<BR>”标签在文本框中强制换行。我为此苦苦挣扎了一段时间,终于找到了这个解决方案。它非常适合我。
回答by Jason
Here's how it's done.
这是它的完成方式。
Readable version:
可读版本:
S=escape(The_textarea.innerHTML);
S=S.replace(/%0D%0A/g,'<br>');
S=S.replace(/%0A/g,'<br>');
S=S.replace(/%0D/g,'<br>');
S=unescape(S);
Efficient version:
高效版:
S=unescape(escape(The_textarea.innerHTML).replace(/%0D%0A/g,'<br>').replace(/%0A/g,'<br>').replace(/%0D/g,'<br>'));
回答by cssyphus
The \n\r
carriage returns are still there, they just are not being interpreted (changed into HTML).
该\n\r
回车仍然存在,他们只是没有被解释(变成HTML)。
Therefore, use <br />
elements - on the client side they will be interpreted as HTML and will display line breaks as desired.
因此,使用<br />
元素 - 在客户端,它们将被解释为 HTML 并根据需要显示换行符。
回答by Joseph Marikle
alert($('.txtclass').val().replace(/\r/g,"\r").replace(/\n/g,"\n"));
have you tried something like that?
你试过这样的吗?