简体   繁体   English

在JavaScript代码中使用换行符检索数据时出现Textarea问题

[英]Textarea issues when retrieving data with line breaks inside javascript code

A lot of queries raised targeting this issue. 针对此问题提出了许多查询。 ie line breaks within a textarea attribute. 即在textarea属性内换行。

I tried to use str_replace("<br />", "\\n",$text) within a javascript variable where I got it working within a php code. 我试图在JavaScript变量中使用str_replace("<br />", "\\n",$text) ,使其在php代码中正常工作。 Unfortunatley had no such luck with the methodology I am using within the javascript code. 对于我在javascript代码中使用的方法学,Unfortunatley并不满意。

The code I am trying to use is as follows: 我尝试使用的代码如下:

var markup = "<textarea name='tcaction[]' id='tcaction' rows='3' cols='105' placeholder='Enter Required Actions' required><?php echo str_replace("<br />", "\n",$text) ?></textarea><br>";

The str_replace within the javascript variable is not working. javascript变量中的str_replace无法正常工作。 Would you cordially direct me to the right direction? 您能真诚地指引我正确的方向吗?

Thanks for assistance. 感谢您的协助。

Replace this ... 替换这个...

...<?php echo str_replace("<br />", "\n",$text) ?>...
                                     ^^

... by this: ... 这样:

...<?php echo str_replace("<br />", "\\n",$text) ?>...
                                     ^^^

So PHP sends \\n (2 chars) and JavaScript interprets it as newline character. 因此,PHP发送\\n (2个字符),JavaScript将其解释为换行符。

All, for the interest of others the issue has been resolved as follows: 所有人,为了他人的利益,已解决了以下问题:

var text = <?php echo json_encode($text); ?>

and then used 然后用

text.replace("<br />", "\\n");

it works and no issues of un-escaped new line etc etc. 它可以正常工作,并且不会出现未转义的新行等问题。

thanks anyway. 不管怎么说,还是要谢谢你。

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

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