简体   繁体   English

Textarea值不保留变量的换行符

[英]Textarea value not retaining line breaks from variable

I have a textarea that needs to retain the submitted value and format after submission, but for some reason \\n does not seem to get passed from the variable php variable. 我有一个textarea需要在提交后保留提交的值和格式,但由于某种原因\\ n似乎没有从变量php变量传递。

$text = '1\n2\n3';

echo '<script>
var text = "'.$text.'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'

The variable is passed from php to JS. 变量从php传递给JS。 When I echo the variable I get the expected result: 当我回显变量时,我得到了预期的结果:

1\n2\n3

When the variable is assigned to the textarea value the result is: 将变量赋值给textarea值时,结果为:

123

Instead of: 代替:

1
2
3

Now if I change the js to the following I get the expected result with each number on a new line. 现在,如果我将js更改为以下内容,我会在新行上获得每个数字的预期结果。

$("textarea").val("1\n2\n3");

Does anyone know why the php variable is not passing the \\n returns to JS? 有谁知道为什么php变量没有传递\\ n返回JS?

Using json encode should escape your line breaks in a way JS will understand. 使用json编码应该以JS将理解的方式逃避您的换行符。

$text = json_encode('1\n2\n3');

echo '<script>
var text = JSON.parse("'.$text.'");
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'

You should use this. 你应该用它。

    <script   src="https://code.jquery.com/jquery-3.1.1.min.js"   integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="   crossorigin="anonymous"></script>

<textarea rows="4" cols="50">
</textarea>


<?php

$text = '1\n2\n3';

echo '<script>
var text = "'.$text.'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'
?>

在此输入图像描述

The problem is you are expecting Browser Javascript to understand PHP's "\\n" character. 问题是你期望浏览器Javascript能够理解PHP的“\\ n”字符。 That will not work. 那样不行。 Also, your code uses the single quote to denote the newline character. 此外,您的代码使用单引号来表示换行符。 PHP requires that you use double quotes to denote the newline special character. PHP要求您使用双引号来表示换行特殊字符。

There are at least two ways you can fix this. 至少有两种方法可以解决这个问题。

The first is to use PHP's nl2br() function. 第一种是使用PHP的nl2br()函数。 In that case, your code could be rewritten as: 在这种情况下,您的代码可以重写为:

$text = "1\n2\n3"; //notice the use of double quotes.

echo '<script>
var text = "'. nl2br($text).'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'

A second way is to use the HTML 第二种方法是使用HTML
tag directly in your variable declaration. 直接在变量声明中标记。 This would obviate the need to use PHP's nl2br() function. 这样可以避免使用PHP的nl2br()函数。

$text = '1<br>n2<br>n3';

echo '<script>
var text = "'.$text.'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'

Either of both approaches should work. 这两种方法都应该有效。

$text = '1\n2\n3';

change to double quote sign: 更改为双引号:

$text = "1\n2\n3";

In PHP double quote " and single quote ' not parse equal. For something we need use double quote, like this. 在PHP双引号“和单引号”不解析相等。对于我们需要使用双引号的东西,像这样。

For: $text = "\\n1\\n2\\n3\\n"; 对于:$ text =“\\ n1 \\ n2 \\ n3 \\ n”;

Source Page view would be: 源页面视图将是:

<script>
var text = "
1
2
3
";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>

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

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