简体   繁体   English

在HTML中使用JS输入NewLine

[英]Enter NewLine with JS in HTML

I have 3 inputs: 我有3个输入:

<input type="text" id="id-1">
<input type="text" id="id-2">
<input type="text" id="id-3">

I have a button which calls a function: 我有一个调用函数的按钮:

<button onclick="myfunction()">click me</button>

This is the JS-function which gets called, it gets the value of all inputs and displays the whole value in a textarea: 这是被调用的JS函数,它获取所有输入的值并在文本区域中显示整个值:

 function myfunction() { var x = "MyText1: " + document.getElementById("id-1").value + "MyText2: " + document.getElementById("id-2").value + "MyText3: " + document.getElementById("id-1").value; document.getElementById("mytext").innerHTML = x; } 

This is the area where the functions shows the text of the input fields: 这是函数显示输入字段文本的区域:

<textarea id="mytext"></textarea>

So far so good. 到现在为止还挺好。 The textarea looks like this: 文本区域如下所示:

"MyText1: input of id-1" "MyText2: input of id-2" "MyText3: input of id-3"

What I want to achieve is the following output: 我想要实现的是以下输出:

    "MyText1: input of id-1"
    "MyText2: input of id-2"
    "MyText3: input of id-3"

My problem is, when I add document.write("\\n"); 我的问题是,当我添加document.write("\\n"); to the script, the page crashs with following console text: Uncaught TypeError: Cannot read property 'value' of null . 到脚本,页面崩溃并显示以下控制台文本: Uncaught TypeError: Cannot read property 'value' of null

Here a working demo without newline: https://jsfiddle.net/6nw8jrk9/ 这里是一个没有换行符的工作演示: https : //jsfiddle.net/6nw8jrk9/

You can add a newline character ( \\n ) to the end of each line when you build your string x like so: 像这样构建字符串x时,可以在每行末尾添加换行符( \\n ):

var x =
    "MyText1: " + document.getElementById("id-1").value + '\n' +
    "MyText2: " + document.getElementById("id-2").value + '\n' +
    "MyText3: " + document.getElementById("id-1").value;

See working example below: 请参见下面的工作示例:

 function myfunction() { var x = "MyText1: " + document.getElementById("id-1").value + '\\n' + "MyText2: " + document.getElementById("id-2").value + '\\n' + "MyText3: " + document.getElementById("id-1").value; document.getElementById("mytext").innerHTML = x; } 
 <input type="text" id="id-1"> <input type="text" id="id-2"> <input type="text" id="id-3"> <br /> <textarea id="mytext"></textarea> <button onclick="myfunction()">click me</button> 

Alternatively, you can use ES6's template literals : 另外,您可以使用ES6的模板文字

 function myfunction() { var x = `MyText1: ${ document.getElementById("id-1").value} MyText2: ${document.getElementById("id-2").value} MyText3: ${document.getElementById("id-1").value}`; document.getElementById("mytext").innerHTML = x; } 
 <input type="text" id="id-1"> <input type="text" id="id-2"> <input type="text" id="id-3"> <br /> <textarea id="mytext"></textarea> <button onclick="myfunction()">click me</button> 

You should append \\n after each input value. 您应该在每个输入值后附加\\n

<script>
  function myfunction() {
    var x =
    "MyText1: " + document.getElementById("id-1").value + '\n' +
    "MyText2: " + document.getElementById("id-2").value + '\n' +
    "MyText3: " + document.getElementById("id-1").value;

    document.getElementById("mytext").innerHTML = x; 
   }
</script>

You can just add line breaks to your var x 您可以在var x添加换行符

  var x =
  "MyText1: " + document.getElementById("id-1").value + '\n' +
  "MyText2: " + document.getElementById("id-2").value + '\n' +
  "MyText3: " + document.getElementById("id-1").value;

You can break a line into an textarea with \\n or with the HTML entitie &#13;&#10; 您可以使用\\n或HTML实体&#13;&#10;将行分成文本区域&#13;&#10;

 var x = "MyText1: " + "valueInput" + "\\n" + "MyText2: " + "valueInput" + "&#13;&#10;" + "MyText3: " + "valueInput"; document.getElementById("mytext").innerHTML = x; 
 <textarea id="mytext"></textarea> 

use document.getElementById("mytext").value = x; 使用document.getElementById("mytext").value = x;

eg: 例如:

  <input type="text" id="id-1" value="aa" /> <input type="text" id="id-2" value="bb" /> <input type="text" id="id-3" value="cc" /> <textarea id="mytext"></textarea> <button onclick="myfunction()">click me</button> <script> function myfunction() { var x = "MyText1: " + document.getElementById("id-1").value + "\\nMyText2: " + document.getElementById("id-2").value + "\\nMyText3: " + document.getElementById("id-3").value; document.getElementById("mytext").value = x; } </script> 

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

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