2016-01-08 94 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>