简体   繁体   English

如何使用javascript从多个文本输入中打印数组列表?

[英]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.

相关问题 我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值? - How do i use JavaScript to take the input from a text box to a variable then print the value of a variable? 如何使用javascript打印带有上划线的文本 - How do I print overlined text using javascript 如何使用 JavaScript 获取文本输入字段的值? - How do I get the value of text input field using JavaScript? 如何使用javascript输入变量以更改标签文本 - How do I input variables to change label text using javascript 如何使用Javascript将字符发送到文本输入 - How do I send Chars to a text Input using Javascript 如何使用 JavaScript 对文本输入执行验证检查? - How do I perform a validation check to a text input using JavaScript? 如何通过HTML表单输入文本并使用Javascript从JSON文件中检索数据作为响应? - How do I input text via an HTML form and retrieve data from a JSON file as a response using Javascript? 我如何在javascript中打印偶数个子数组 - how do i print even numbers of sub arrays in javascript 如何使用 JavaScript 动态设置输入列表下拉列表? - How do I set input list dropdown dynamically using JavaScript? 如何使用 html 输入字段从 php 代码显示 javascript 数组中的列表? - How do i show a list in a javascript array from a php code using a html input field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM