[英]How to Access an array of objects properties?
i'm working in a simple program that uses an HTML Form to fill an Array with some information, so far, i can get the input data, store it into my list, and visualize it like this : 我正在一个简单的程序中工作,该程序使用HTML表单将一些信息填充到数组中,到目前为止,我可以获取输入数据,将其存储到我的列表中,并像这样可视化它:
It basically, converts the Name to a Link, when you click it, it will create a <div>
in which i show all the information of the contact. 基本上,它将名称转换为链接,当您单击它时,它将创建一个
<div>
,其中我显示联系人的所有信息。
And i've done this like this : 我这样做是这样的:
The error i'm getting is in the last 6 lines of Code. 我得到的错误是在代码的最后6行中。
(I'm trying to avoid all non-troubling code) (我试图避免所有非麻烦的代码)
var list = [];
var ulList = document.createElement("UL");
function AddToList(){
//Just pushes the info into the list.
}
function Visualize(){
ClearScreen();
for(var i = 0 ; i < list.length ; i++){
//Tried to keep it clean, this just works with each item in the list.
AddToList(i);
}
}
//This Works correctly, it shows every Name i've previously pushed into the list like a Link.
function AddToList(index){
var element = document.createElement("LI");
var name = document.createTextNode(list[index].name);
element.appendChild(name);
var link = document.createElement("A");
link.setAttribute("HREF", "#");
link.appendChild(element);
lik.setAttribute("ID", index);
link.addEventListener("click", ShowInfo(this.id)); //Index would do the same
ulList.appendChild(link);
document.getElementsByTagName("body")[0].appendChild(ulList);
}
//Trouble comes here
function ShowInfo(index){
CleanDIV();
//Previously created <div> with box as id
var box = document.getElementById("box");
var details = document.createElement("UL");
var lName = document.createElement("LI");
var lNumber = document.createElement("LI");
var lMail = document.createElement("LI");
//
//The error is here : Cannot Read Property 'name' of undefined
//And i dont know why, since i've done something similar in a previous line...
//
lName.appendChild(document.createTextNode("Name :" + list[index].name));
lNumber.appendChild(document.createTextNode("Number : " + list[index].number));
lMail.appendChild(document.createTextNode("Mail : " + list[index].mail));
details.appendChild(lName);
details.appendChild(lNumber);
detaisl.appendChild(lMail);
}
I dont even know what kind of mistake i'm making, and was not sure of how to ask this question. 我什至不知道我犯了什么样的错误,并且不确定如何问这个问题。
I apologyze for any grammar mistake, my bad variable naming abilities and any lack of quality in my question. 我为任何语法错误,错误的变量命名能力以及问题中的任何质量欠缺而道歉。
Thank you kindly. 非常感谢你。
在您的AddToList函数中,索引是“未定义的”
You get the error because you have not added anything to your list
array. 因为没有将任何内容添加到
list
数组,所以收到错误消息。 Add the person details to the list
array when the Add to List button is clicked. 单击“ 添加到列表”按钮时,将人员详细信息添加到
list
数组。 And retrieve this when the link is clicked. 并在单击链接时检索它。
var name = document.getElementById("txtName").value;
var number = document.getElementById("txtNnumber").value;
...
var person = {'name': name, 'number': number};
list.push(person);
Looks like you have a couple of typos in your code. 看起来您的代码中有几次错别字。
lik.setAttribute("ID", indice);
link.addEventListener("click", ShowInfo(this.id)); //Index would do the same
should be 应该
link.setAttribute("ID", index);
link.addEventListener("click", ShowInfo(index)); //Index would do the same
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.