【发布时间】:2018-10-27 05:14:09
【问题描述】:
我有一个 HTML 表单。来自表单的输入数据将进入我在 JS 中制作的多维数组。然后我将使用 for 循环访问数组中的对象,以便在 HTML div 中显示对象值。 html中只显示[object,object]
var data = [];
var i, item;
function myForm(event){
event.preventDefault();
var name = document.getElementById("name").value; //Getting Values from the form of HTML
var phone = document.getElementById("phone").value; //Getting Values from the form of HTML
var bday = document.getElementById("bday").value; //Getting Values from the form of HTML
var email = document.getElementById("email").value; //Getting Values from the form of HTML
var pWord = document.getElementById("pWord").value; //Getting Values from the form of HTML
var age = document.getElementById("bday").value; //Getting Values from the form of HTML
var ageValue;
var Bdate = document.getElementById("bday").value; //Getting Values from the form of HTML and calculating the age of the user
var Bday = +new Date(Bdate);
ageValue = ~~ ((Date.now() - Bday) / (31557600000));
var theBday = document.getElementById("age");
theBday.innerHTML = ageValue;
var userObject = {
name: name,
phone: phone,
bday: bday,
email: email,
pWord: pWord,
ageValue: ageValue,
}; //The values i get from my Input in html. The userObject will be in a array data[]
data.push(userObject);
for (i=0 ; i <data.length ; i++){
for (item in data[i]){
document.getElementById("demo3").innerHTML=(item + data[i][item]); //I'm trying to display the details from my form but the only thing show up is [object,object]
}
}
}
【问题讨论】:
-
你能发布你的html
-
@mabhijith95a10
-
@mabhijith95a10 抱歉,我无法将其作为代码发布,
标签: javascript html arrays object multidimensional-array