我似乎无法在阵列中创建另一个用户输入值。每次我想在数组中添加新的表单值时,它只会覆盖数据。这个代码有什么其他方法可以工作吗?不使用js库和框架。 这是我的代码。谢谢。
function array()
{
var fName = document.getElementById('fullName').value;
var userName = document.getElementById('username').value;
var elmail = document.getElementById('email').value;
var pword = document.getElementById('password').value;
var b_day = document.getElementById('bday').value;
var g_nder = document.getElementById('gender').value;
var storage = [];
var person = [];
person[userName] = {
"Full Name" : fName,
"Email" : elmail,
"Password" : pword,
"Birthday" : b_day,
"Gender" : g_nder
};
storage.push(person);
console.log(storage);
}
答案 0 :(得分:1)
将函数外部的存储保留为全局变量。
var storage = [];
function array()
{
var fName = document.getElementById('fullName').value;
var userName = document.getElementById('username').value;
var elmail = document.getElementById('email').value;
var pword = document.getElementById('password').value;
var b_day = document.getElementById('bday').value;
var g_nder = document.getElementById('gender').value;
var person = [];
person[userName] = {
"Full Name" : fName,
"Email" : elmail,
"Password" : pword,
"Birthday" : b_day,
"Gender" : g_nder
};
storage.push(person);
console.log(storage);
}
您每次都要将存储初始化为空数组