[英]Storing contents of textarea with line breaks
I'm trying to store the contents of a textarea into localStorage including line breaks. 我正在尝试将textarea的内容存储到localStorage中,包括换行符。
<textarea cols="40" rows="8" name="delivery-address-input" id="delivery-address-input" required></textarea>
Imagine the contents of the text area are: 想象一下文本区域的内容是:
1234 Smith Street Dunedin New Zealand
$('#delivery-address-input').val()
returns the contents including the linebreaks. $('#delivery-address-input').val()
返回包含换行符的内容。
But when I try: 但是当我尝试:
localStorage.setItem("contact-address", $('#delivery-address-input').val();
And check the contents: 并检查内容:
localStorage.getItem("contact-address")
The linebreaks are all truncated 换行符都被截断
1234Smith StreetDunedinNew Zealand 史密斯街1234号但尼丁新西兰
Try 尝试
var val = $('#delivery-address-input').val().replace(/\n\r?/g, '<br/>');
localStorage.setItem("contact-address", val);
textarea line breaks are like this \\n\\r
and html is <br/>
so replace textarea line breaks to html ones . textarea换行符是这样的
\\n\\r
而html是<br/>
所以将textarea换行符替换为html。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.