简体   繁体   English

javascript使用新行设置textarea值

[英]javascript setting textarea value with new line

i am new to php, javascript and web我是 php、javascript 和 web 的新手

I am having a situation => I have to set a value of text field on my client side, i pass value to my javascript function and it sets value as我遇到了一种情况 => 我必须在客户端设置文本字段的值,我将值传递给我的 javascript 函数并将值设置为

 function editSubService(description){
      setVal("sub_service_description", description);
 } 

and it sets the value right , but if there is new line in the text, it does not set the text field.并将值设置为 right ,但如果文本中有新行,则不会设置文本字段。 how can this set that value of text field, its only not working if new line这如何设置文本字段的值,只有在换行时才不起作用

A <input> element with type="text" will not accept any newline characters. type="text"<input>元素不接受任何换行符。 It is, by definition, a single-line field.根据定义,它是一个单行字段。
Use a <textarea> if you wish to have a multi-line input.如果您希望多行输入,请使用<textarea>

It's specification for type="text" , according to MDN :根据MDN ,它是type="text"的规范:

text: A single-line text field; text:单行文本字段; line-breaks are automatically removed from the input value.换行符会自动从输入值中删除。

Example:例子:

 var area = document.getElementById("sub_service_description"); area.value = "This is some\\ntext with a newline character"; console.log(area.value);
 <textarea id="sub_service_description"></textarea>​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM