簡體   English   中英

jQuery-.val到文本區域,不保留換行符

[英]jQuery - .val to a textarea not preserving linebreaks

不知道為什么我的textarea沒有接換行符。 我正在嘗試使用文本模板預填充文本區域,但我需要它來保留換行符,而不是將其放在一行中。 有人知道我在想什么嗎?

這是一個示例https://jsfiddle.net/t8y1okpp/

這是我的HTML,非常簡單:

<textarea class="form-control" rows="5" id="Revue" style="width: 100%; min-height: 200px; white-space: pre;"></textarea>

這是我的JavaScript:

var revue_text_template ="Multi-Client impact? \
1: \
2: \
3? \

4? \\ 5?“; var revue_text_template_filtered = revue_text_template.replace(/ \\ r \\ n | \\ r | \\ n / g,” \\ n“);

$('#Revue').val(revue_text_template_filtered);

您應該使用\\n作為換行符。

var revue_text_template = "Multi-Client impact? \nPrime Speaker(s): \n Component affected (infrastructure, application, server, network, etc.): \n Root Cause identified? If yes, what is the cause? \n How was the incident detected (alarm, client, vendor)? \n Was the incident caused by a planned change? If yes, what is the change number? \n Was recovery optimal? If not, why? \n Issues/gaps encountered?";
$('#Revue').val(revue_text_template);

更新小提琴

或者,您也可以使用javascript heredoc創建模板字符串,例如。

var revue_text_template = `Line 0
Line 1,
Line 2,
Line 3,
Line 4`;

該字符串只是一個普通的單行字符串,分為多行。 因此,它不包含\\r和/或\\n字符。

使用ES6 模板字符串

更新小提琴

 var revue_text_template = `Multi-Client impact? Prime Speaker(s): Component affected (infrastructure, application, server, network, etc.): Root Cause identified? If yes, what is the cause? How was the incident detected (alarm, client, vendor)? Was the incident caused by a planned change? If yes, what is the change number? Was recovery optimal? If not, why? Issues/gaps encountered?`; $('#Revue').val(revue_text_template); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <textarea class="form-control" rows="5" id="Revue" style="width: 100%; min-height: 200px; white-space: pre;"> </textarea> 


ES5中 ,可以使用Array#join

小提琴

 var revue_text_template = ["Multi-Client impact?", "Prime Speaker(s):", "Component affected(infrastructure, application, server, network, etc.):", "Root Cause identified ? If yes, what is the cause ?", "How was the incident detected(alarm, client, vendor) ?", "Was the incident caused by a planned change ? If yes, what is the change number ?", "Was recovery optimal ? If not, why ?", "Issues / gaps encountered ?" ]; $('#Revue').val(revue_text_template.join("\\n")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <textarea class="form-control" rows="5" id="Revue" style="width: 100%; min-height: 200px; white-space: pre;"></textarea> 

更新的小提琴

在\\ r \\ n中添加了換行符。

var revue_text_template ="Multi-Client impact? \r\n\
Prime Speaker(s): \r\n\
Component affected (infrastructure, application, server, network, etc.): \r\n\
Root Cause identified? If yes, what is the cause? \r\n\
How was the incident detected (alarm, client, vendor)? \r\n\
Was the incident caused by a planned change? If yes, what is the change     number? \r\n\
Was recovery optimal? If not, why? \r\n\
Issues/gaps encountered?";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM