javascript Jquery,textarea 中的换行符 .. 没有得到 \n 并得到 ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18425575/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:53:05  来源:igfitidea点击:

Jquery, line break in textarea.. don't get \n and get ?

javascriptjqueryajaxtextarealine-breaks

提问by Jhonatan G.

I am trying to get value of a textarea with line break. When I debug, the value is this way in jquery. the value stored in a variable like this:

我正在尝试使用换行符获取 textarea 的值。当我调试时,jquery 中的值是这样的。存储在变量中的值,如下所示:

"test<br>
test<br>
test"<br>
In .value of valueOf is: 'test?test?test'.

I wonder how can I convert it to \n in order to insert line break.. I'm using jquery to get the value and send by ajax to php!

thanks.. :)
Sorry my english..

我想知道如何将它转换为 \n 以插入换行符..我正在使用 jquery 获取值并通过 ajax 发送到 php!

谢谢.. :)
对不起我的英语..

回答by rakeshjain

Try this

试试这个

val=document.getElementById('recommend').value;
val = val.replace(/<br\s*\/?>/mg,"\n");

Fiddle http://jsfiddle.net/eSub4/1/

小提琴http://jsfiddle.net/eSub4/1/

I have put a couple of console.log in fiddle to show you how new line(\n) and html line break show up in console and compare it with the text from textarea to see that you are getting new line (\n) for text in textarea after using the regex. Keep firebug open to see the output

我在 fiddle 中放了几个 console.log 来向您展示新行(\n)和 html 换行符如何显示在控制台中,并将其与 textarea 中的文本进行比较,以查看您正在获取新行(\n)使用正则表达式后 textarea 中的文本。保持萤火虫打开以查看输出

回答by chiliNUT

try

尝试

document.getElementById('textareaid').innerHTML;

you need to replace 'textareaid' with the actual id. since you say you already have the data in a string, but its not formatted right, to turn the <br>into newlines, you can use this

您需要用实际 ID 替换 'textareaid'。既然你说你已经有一个字符串中的数据,但它的格式不正确,把它<br>变成换行符,你可以使用这个

textString=textString.replace(/<br>/g,"\n");

回答by Sam Asir

It works for me....

这个对我有用....

$("#id").val("<?php echo str_replace(array("\r\n","\r","\n","\r","\n","\r\n"),"<br>",$value); ?>".replace(/<br\s*[\/]?>/gi, "\n"));