本文介绍了重新发布&lt;输入&gt;<textarea> 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将用户的输入值重新生成到具有特定格式的 textarea 上.我已经创建了输入和选择",里面有各种选项"以及触发该功能的按钮".

I want to re-generate user's input values onto textarea with specific format.I've created input and 'select' with various 'options' inside as well as 'button' that triggers the function.

谁能告诉我我在这里做错了什么..?

Can someone please tell me what I'm doing wrong here..?

<SCRIPT>

function myFunction() {
   var finaL = document.getElementById("textArea").value;
   var spacE = " | ";

   var a = document.getElementById("input1").value;
   var b = document.getElementById("input2").value;
   var c = document.getElementById("selecT").value;

   finaL = a + spacE + b + spacE + c;
}

</SCRIPT>

推荐答案

在您的代码中,您将获取 textarea 的值并将其存储在 final 变量中,该变量只是一个字符串.您需要做的是获取final 变量中textarea 的引用,然后设置值.

In your code your are getting the value of the textarea and storing it in final variable which is just a string. What you need to do is get the reference of the textarea in the final variable and then set the value.

工作代码:

function myFunction() {
    var finaL = document.getElementById("textArea");
    var spacE = " | ";
    var a = document.getElementById("input1").value;
    var b = document.getElementById("input2").value;
    var c = document.getElementById("selecT").value;
    finaL.value = a + spacE + b + spacE + c;
}
<label for="input1">Male</label>
<input name="input1" id="input1" /> <br>
<label for="input2">Input 3</label>
<input name="input2" id="input2" /> <br>
<label for="selecT">Input 3</label>
<select id="selecT">
    <option value="Value 1">Value 1</option>
    <option value="Value 2">Value 2</option>
    <option value="Value 3">Value 3</option>
</select>
<br>
<button type="button" onclick="myFunction()">Copy</button>
<br>
<label>Result</label>
<textarea id="textArea"></textarea>

这篇关于重新发布&lt;输入&gt;<textarea> 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:07