从文本框中读取输入并将其存储在 JavaScript 中的数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13938480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading an input from a textbox and storing it in an array in JavaScript
提问by beccas_100
I have a <textarea>
that allows text to be inputted in the form of a string. Then the users clicks on a button which displays what they have inputted back to them in a text area within a table.
我有一个<textarea>
允许以字符串的形式输入文本。然后用户单击一个按钮,该按钮在表格的文本区域中显示他们输入的内容。
I need to use an array to store what has been inputted by the user, and then display it back out into another <textarea>
element within a table from the array, where the user input is stored from the input box.
我需要使用一个数组来存储用户输入的内容,然后将其显示回<textarea>
数组中表中的另一个元素,其中用户输入是从输入框存储的。
Any pointers on how to fill up an array and stacks, from a user input would be great.
任何关于如何从用户输入填充数组和堆栈的指针都会很棒。
回答by jose
You can declare your array like this
你可以像这样声明你的数组
var yourArray = new Array();
or
或者
var yourArray = [];
To add items to array:
将项目添加到数组:
yourArray.push(yourString);
To get you can use indexing like (almost any other language)
为了让您可以使用索引(几乎任何其他语言)
yourArray[i]
You can even set as an object array like this:
你甚至可以像这样设置一个对象数组:
yourArray.push({ text : 'blablabla'})
So, in your case, filling up the array could be something like this:
所以,在你的情况下,填充数组可能是这样的:
var inputText = document.getElementById('id_of_input').value;
yourArray.push(inputText);
// show it
for(var i=0; i<yourArray.length; i++) {
alert(yourArray[i]);
}