2016-01-08 143 views
0

不知道爲什麼我的textarea沒有選擇換行符。我試圖預先填充textarea文本模板,但我需要它來保留換行符而不是在一行中。有人會知道我錯過了什麼嗎?jQuery - .val到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); 
+2

的字符串不包含'\ r'和/或'\ n ',這是正常的單行字符串,它在多行中斷。使用ES6多行字符串[Demo](https://jsfiddle.net/tusharj/t8y1okpp/6/) – Tushar

回答

2

您應該使用\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); 

Updated Fiddle

或者,您可以使用JavaScript定界符創建模板字符串,如..

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

我非常喜歡替代方法,它非常簡單。非常感謝。 –

+0

@DanielEllison噢,真的,我想你沒有看到我的答案是在這之前10分鐘發佈的。我的評論也是關於這個問題的。 – Tushar

0

Updated fiddle

添加在\ 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?"; 
1

該字符串是剛剛被分裂多行,所以它不包含\r和/或\n字符的正常單一行字符串。

使用ES6Template strings

Updated Fiddle

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

Fiddle

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>