我正在尝试创建“客户”对象,该对象将存储所有这些数据,然后将信息显示为“客户订单”,并列出所有新信息。我想一定是这样的。
localhost:8480/app/assets/images/logo.png
但是,对我来说主要的问题是我无法弄清楚如何存储所有这些数据,然后将这些信息显示为“客户订单”并列出所有新信息。
答案 0 :(得分:1)
var customerobject={name,address,postalcode,phone,email};
不正确。实际上,您只是在为对象提供没有值的键。 它应该是这样的:
var customerobject = {name: "",address: "",postalcode: "",phone: "",email: ""}
以下是基于您的代码的示例:
var objectarray = [];
function addToArray() {
var customerobject = {
name: "",
address: "",
postalcode: "",
phone: "",
email: ""
}
customerobject.name = document.getElementById("name").value;
customerobject.address = document.getElementById("address").value;
customerobject.postalcode = document.getElementById("postalcode").value;
customerobject.phone = document.getElementById("phone").value;
customerobject.email = document.getElementById("email").value;
objectarray.push(customerobject);
console.log(objectarray);
}
document.getElementById("clickMe").addEventListener("click", clicked);
function clicked() {
addToArray();
}
<input id="name" value="Gregory" />
<input id="address" value="Mapstreet" />
<input id="postalcode" value="56467454" />
<input id="phone" value="1234" />
<input id="email" value="me@myself.com" />
<button id="clickMe">click me</button>