在json中存储具有换行符的字符串时出现问题



我有一个文本框,我将其值存储在json中,如下所示

description=JSON.stringify($("#textarea").val());

然后我将它存储在json对象中,如下所示

myObj['description']=description;

Whole Json现在存储在我的mysql数据库中。每当我试图获取整个json并用描述重新填充文本区域时,我都会遇到以下问题:字符串

Your Name Is ABCnYou Live innYour Phone number Isn

基本上现在我的文本区域不识别换行

您可以使用类似的jquery设置html内容

$(document).ready(function(){
$("button").click(function(){
$("#loading-area").html('Your Name Is ABCnYou Live innYour Phone number Isn').wrap('<pre />');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<body>
<button>Change content of all p elements</button>
<p id="loading-area">This is the loading para.</p>

在JSON中存储字符串时,new line存储为未渲染的n字符。

textarea:中渲染new line

$('#id-for-element').html(myObj['description'].replaceAll('n', '&#13;&#10;'));

HTML将解析&#13;&#10;并将其呈现为new line
HTML字符代码

最新更新