如何将输入字段中的字符串值存储在空数组中?

时间:2021-09-09 07:18:52

I'm having trouble storing input values from <input> tag in HTML into array of strings, I can't figure out how am I suppose to do that. I have an idea on how that might look like, however I still can't get it to work.

我无法将HTML中标签的输入值存储到字符串数组中,我无法弄清楚我是怎么想的。我对这可能是什么样子有一个想法,但是我仍然无法让它发挥作用。

I believe that I have to use .push() and .join() method and += or + operator, it's just I do not know where to put them.

我相信我必须使用.push()和.join()方法以及+ =或+运算符,这只是我不知道把它们放在哪里。

The first thing I did was searching on Google How to store string value from input in an array of strings? but I only found on how to do it using <form> tag in HTML and I can't do that. I can't use the <form> tag.

我做的第一件事是在Google上搜索如何在字符串数组中存储输入的字符串值?但我只是在HTML中使用

标签找到了如何做到这一点,我不能这样做。我不能使用标签。

Here's the code that I think should look like

这是我认为应该看起来的代码

<body>
  <input type="text" id="input" />
  <button onclick="submit()">Submit</button>
  <div id="placeholder"></div>
</body>

var inputName = document.getElementById("input");
var cityArray = [""];

// This triggers immediately when the browser loads
window.onload = (
  // Pickup the string from input and add it on the previously created array
  function submit() {
    inputName.value;

    for (var i = 0; i < array.length; i++) {
      array[i];
    }
  }
);

I also need a piece of code that will delete the value that was typed in a <input> field right after the Submit button is pressed, so that the user doesn't need to press Backspace in order to type the second input value.

我还需要一段代码,它将在按下Submit按钮后立即删除在字段中键入的值,这样用户无需按Backspace键即可键入第二个输入值。

3 个解决方案

#1


2  

Here is a working code snippet.

这是一个有效的代码片段。

When you click the submit button, that will call the submit() function. Since your array is defined to be global, you can access it within the function. You do not need to iterate over the array, and you can simply use the .push() method to easily append a string to your array.

单击提交按钮时,将调用submit()函数。由于您的数组被定义为全局数组,因此您可以在函数中访问它。您不需要遍历数组,只需使用.push()方法就可以轻松地将字符串附加到数组中。

var inputName = document.getElementById("input");
var cityArray = [];


function submit() {
  cityArray.push(inputName.value);
  console.log(cityArray);
}
<body>
  <input type="text" id="input" />
  <button onclick="submit()">Submit</button>
  <div id="placeholder"></div>
</body>

Hope this helps!

希望这可以帮助!

#2


1  

Yes you need to use .push() method it will add the new entered string to the array, without the need to iterate it:

是的,您需要使用.push()方法,它会将新输入的字符串添加到数组中,而无需迭代它:

function submit() {
   cityArray.push(inputName.value);

}

And you need to initialize your array as an empty array with [] :

您需要使用[]将数组初始化为空数组:

var cityArray = [];

And you don't need to create the submit function in the body.onload event handler because it won't be accessible outside of it and may lead for an error.

而且您不需要在body.onload事件处理程序中创建提交函数,因为它不能在其外部访问,并可能导致错误。

Demo:

var inputName = document.getElementById("input");
var cityArray = [];


function submit() {
  cityArray.push(inputName.value);
  console.log(cityArray);
}
<input type="text" id="input" />
<button onclick="submit()">Submit</button>
<div id="placeholder"></div>

#3


0  

Js Code

//declare your array like this
var yourArray = new Array();
//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('input').value;
yourArray.push(inputText);
// show it
for (var i = 0; i < yourArray.length; i++) {
  alert(yourArray[i]);
}

HTML Script

<body>
  <input type="text" id="input" />
  <button onclick="submit()">Submit</button>
  <div id="placeholder"></div>
</body>

#1


2  

Here is a working code snippet.

这是一个有效的代码片段。

When you click the submit button, that will call the submit() function. Since your array is defined to be global, you can access it within the function. You do not need to iterate over the array, and you can simply use the .push() method to easily append a string to your array.

单击提交按钮时,将调用submit()函数。由于您的数组被定义为全局数组,因此您可以在函数中访问它。您不需要遍历数组,只需使用.push()方法就可以轻松地将字符串附加到数组中。

var inputName = document.getElementById("input");
var cityArray = [];


function submit() {
  cityArray.push(inputName.value);
  console.log(cityArray);
}
<body>
  <input type="text" id="input" />
  <button onclick="submit()">Submit</button>
  <div id="placeholder"></div>
</body>

Hope this helps!

希望这可以帮助!

#2


1  

Yes you need to use .push() method it will add the new entered string to the array, without the need to iterate it:

是的,您需要使用.push()方法,它会将新输入的字符串添加到数组中,而无需迭代它:

function submit() {
   cityArray.push(inputName.value);

}

And you need to initialize your array as an empty array with [] :

您需要使用[]将数组初始化为空数组:

var cityArray = [];

And you don't need to create the submit function in the body.onload event handler because it won't be accessible outside of it and may lead for an error.

而且您不需要在body.onload事件处理程序中创建提交函数,因为它不能在其外部访问,并可能导致错误。

Demo:

var inputName = document.getElementById("input");
var cityArray = [];


function submit() {
  cityArray.push(inputName.value);
  console.log(cityArray);
}
<input type="text" id="input" />
<button onclick="submit()">Submit</button>
<div id="placeholder"></div>

#3


0  

Js Code

//declare your array like this
var yourArray = new Array();
//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('input').value;
yourArray.push(inputText);
// show it
for (var i = 0; i < yourArray.length; i++) {
  alert(yourArray[i]);
}

HTML Script

<body>
  <input type="text" id="input" />
  <button onclick="submit()">Submit</button>
  <div id="placeholder"></div>
</body>