admin管理员组文章数量:1302450
The objective is to add another item to the array, and make an array like ["Car", "House" , "Boat"] etc.
What is happening is when i console log, i only have one item in my array and not all the values i submit from form.
This is my form
<form onsubmit="guardarNumeros()">
<p>Please insert the items</p>
<input type="text" id="box">
<input type="submit" value="Submit">
</form>
My js
function guardarNumeros(){
var items = [];
boxvalue = document.getElementById('box').value;
items.push(boxvalue);
console.log(items);
}
Thank You !
The objective is to add another item to the array, and make an array like ["Car", "House" , "Boat"] etc.
What is happening is when i console log, i only have one item in my array and not all the values i submit from form.
This is my form
<form onsubmit="guardarNumeros()">
<p>Please insert the items</p>
<input type="text" id="box">
<input type="submit" value="Submit">
</form>
My js
function guardarNumeros(){
var items = [];
boxvalue = document.getElementById('box').value;
items.push(boxvalue);
console.log(items);
}
Thank You !
Share Improve this question asked Nov 2, 2017 at 15:05 Hugo SeleiroHugo Seleiro 2,6576 gold badges30 silver badges44 bronze badges 5 |3 Answers
Reset to default 13Your items
array is within the scope of your guardarNumeros
function and will get declared every time guardarNumeros
is called, if you want it to persist it needs to be outside:
var items = [];
function guardarNumeros() {
boxvalue = document.getElementById('box').value;
items.push(boxvalue);
console.log(items);
}
As mentioned in the comments a form submission will refresh the page by default, to prevent this you need to return false:
<form onsubmit="return guardarNumeros()">
function guardarNumeros() {
boxvalue = document.getElementById('box').value;
items.push(boxvalue);
console.log(items);
return false;
}
http://plnkr.co/edit/mGLCT97QwM8CvD3I5yUp?p=preview
By including the items inside the scope of your functions you were declaring the array every time.
var items = [];
function guardarNumeros() {
boxvalue = document.getElementById('box').value;
items.push(boxvalue);
console.log(items);
return false; // stop submission
}
<form onsubmit="return guardarNumeros()">
<p>Please insert the items</p>
<input type="text" id="box">
<input type="submit" value="Submit">
</form>
You have to declare the array outside the function. because whenever you hit the submit button a new array object is being created and the previous one is being lost because of scope.
You know if we declare a variable inside any function this variable is only visible in this function. Outside this function we can't access it. In your case you declare an array inside the function, then push value to it and also log it. But if you try to access this array from outside the function you will get error if you use strict mode.
Just declare the array in global scope, or also you can pass the array as an argument. this will solve your problem..
var items = [];
function guardarNumeros(){
boxvalue = document.getElementById('box').value;
items.push(boxvalue);
console.log(items);
}
Or use this. But make sure you declare the array anywhere in parent function where you call the function.
function guardarNumeros(items){
boxvalue = document.getElementById('box').value;
items.push(boxvalue);
console.log(items);
}
In this case you also have to check some condition...
本文标签: JavaScriptAdd Value from input box to arrayStack Overflow
版权声明:本文标题:JavaScript - Add Value from input box to array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738509577a2090735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<form onsubmit="return guardarNumeros()">
and end the function withreturn false;
to not start with an empty item each time due to a reloaded page – mplungjan Commented Nov 2, 2017 at 15:08