[英]How do I print the a list of arrays from multiple text input using javascript?
Code description: 代码说明:
Clicking the button (id=a1) will add text input brackets (eg 3 clicks will give 3 text input). 单击按钮(id = a1)将添加文本输入括号(例如,单击3次将输入3个文本)。 I am trying to get the values from all the text inputs and show it on the page,however, my code only prints out the value from the first text input.
我试图从所有文本输入中获取值并将其显示在页面上,但是,我的代码仅从第一个文本输入中打印出该值。 How can I get it to print all the values from all the text inputs?
如何获得它来打印所有文本输入中的所有值?
// add textbox function onclick var a1 = 0; var x = []; function addInput() { document.getElementById('text').innerHTML += "Load magnitude <input type='text' id='a1' value=''/><br />"; a1 += 1; } //Adds inout into list var x function pushData() { //get value from input text var inputText = document.getElementById('a1').value; //append data to the array x.push(inputText); var pval = x; document.getElementById('pText').innerHTML = pval; }
<!--Loadtype selection--> <div class="test"> Load type:<br> <img src="https://upload.wikimedia.org/wikipedia/commons/a/a0/Circle_-_black_simple.svg" width="50 height=" 50 alt="unfinished bingo card" onclick="addInput()" /> <br><br> </div> <p id="text"></p> <button onclick="pushData();">Submit</button> <p id="pText">List from input inserted here!</p> <p id="pText2">value of a1</p>
I have made a couple of improvements to your code, that you can see in the code snippet below. 我对您的代码做了一些改进,您可以在下面的代码片段中看到这些改进。
The essential changes you need to make would be, 您需要进行的基本更改是
Use a class selector instead of ID selector. 使用类选择器代替ID选择器。 When you invoke
document.getElementById
it returns one element having the provided ID. 调用
document.getElementById
它将返回一个具有提供的ID的元素。 Instead you want all the textboxes that were dynamically created. 相反,您需要所有动态创建的文本框。 So you can add a CSS class to the input at the time of creation (
class='magnitude-input'
) and afterwards use it to get all inputs ( document.getElementsByClassName('magnitude-input')
). 因此,您可以在创建时将CSS类添加到输入中(
class='magnitude-input'
),然后使用它来获取所有输入( document.getElementsByClassName('magnitude-input')
)。
Once you get a list of inputs, you can iterate over them and collect their values into an array ( x
in your case). 获得输入列表后,您可以遍历它们并将它们的值收集到一个数组中(在您的情况下为
x
)。 Note that x
should be definied within the function pushData
, otherwise it will retain values previously added to it. 请注意,
x
应该在pushData函数中pushData
,否则它将保留先前添加到其中的值。
Also, the variable a1
seems unnecessary after that. 此外,此后似乎不需要变量
a1
。 So you can remove it. 因此您可以将其删除。
// add textbox function onclick function addInput() { document.getElementById('text').innerHTML += "Load magnitude <input type='text' class='magnitude-input' value=''/><br />"; } //Adds inout into list var x function pushData() { var x = []; //get value from input text var inputs = document.getElementsByClassName('magnitude-input'); for(var i = 0; i < inputs.length; i++) { var inputText = inputs[i].value; //append data to the array x.push(inputText); } document.getElementById('pText').innerHTML = x; }
<!--Loadtype selection--> <div class="test"> Load type:<br> <img src="https://upload.wikimedia.org/wikipedia/commons/a/a0/Circle_-_black_simple.svg" width="50 height=" 50 alt="unfinished bingo card" onclick="addInput()" /> <br><br> </div> <p id="text"></p> <button onclick="pushData();">Submit</button> <p id="pText">List from input inserted here!</p> <p id="pText2">value of a1</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.