最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery, line break in textarea.. don't get n and get ↵ - Stack Overflow

programmeradmin0浏览0评论

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:

"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..

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:

"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..

Share Improve this question edited Aug 25, 2013 at 4:12 Konsole 3,4753 gold badges30 silver badges41 bronze badges asked Aug 25, 2013 at 4:04 Jhonatan G.Jhonatan G. 951 gold badge3 silver badges11 bronze badges 1
  • I think it would be better to store the text as is and then call nl2br() when needed. – Matteo Tassinari Commented Aug 25, 2013 at 6:05
Add a ment  | 

3 Answers 3

Reset to default 3

Try this

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

Fiddle http://jsfiddle/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 pare 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

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

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

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"));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论