简体   繁体   English

尝试将存储在数组中的对象属性显示到 <ul> 元件

[英]Trying to display object properties stored within an array onto a <ul> element

I've just started learning JS and I'm trying to do some basic projects to cement what I've learned from reading and courses and tutorials and whatnot. 我刚刚开始学习JS,并且我正在尝试做一些基础项目来巩固我从阅读,课程和教程以及其他方面学到的知识。 I'm trying to make a contact list which takes 4 inputs: first name, last name, email and phone number. 我正在尝试制作一个包含4个输入项的联系人列表:名字,姓氏,电子邮件和电话号码。 I wrote this part already and passed the arguments into an object within an array. 我已经写了这部分并将参数传递给数组中的对象。 What I can't figure out is how to display the contact object. 我不知道如何显示联系人对象。 I want to try and print each property into a list item within an unordered list but I'm stuck here, either because I don't know enough about DOM manipulation or just because I'm not looking in the right direction 我想尝试将每个属性打印到无序列表中的列表项中,但是我被困在这里,或者是因为我对DOM操作的了解不够,或者仅仅是因为我没有朝正确的方向看

 //this passes the text input as an object to the list array var contactList = { list: [], addNew: function() { this.list.push({ firstName: document.getElementById('firstname').value, lastName: document.getElementById('lastname').value, email: document.getElementById('emailAdd').value, phoneNumber: document.getElementById('phoneNumber').value }); }, }; // this runs the addNew() function and clears the input fields afterwards var handlers = { addContact: function() { contactList.addNew(); document.getElementById('firstname').value = ''; document.getElementById('lastname').value = ''; document.getElementById('emailAdd').value = ''; document.getElementById('phoneNumber').value = ''; // view.displayContact(); }, }; //this is where i'm trying to display the contacts array var view = { displayContact: function() { var contacts = document.getElementById('contactul'); for (var i = 0; i < contactList.list.length; i++) { var li = document.createElement('li'); contacts.appendChild(li); li.innerHTML += contactList.list[i]; }; }, }; 
 <form> First name:<br> <input id="firstname" type="text" name="firstname"> <br> Last name:<br> <input id="lastname" type="text" name="lastname"> <br> Email Address:<br> <input id="emailAdd" type="text"> <br> Phone number:<br> <input id="phoneNumber" type="text"> <br> </form> <button onclick='handlers.addContact()'>Submit</button> <div id='displayContacts'> <ul id='contactul'> </ul> </div> 

This is the desired result. 这是期望的结果。 I just can't figure out how to write it. 我只是不知道怎么写。

Well, you're close. 好吧,你很近。 The problem you have here is that when you go to display your items, you have to get the values for each individual element (first name, last name, etc). 这里的问题是,当您显示商品时,必须获取每个元素的值(名字,姓氏等)。 You can do that through another loop, or just hard-code each one since there are only 4. Here is an example: 您可以通过另一个循环执行此操作,也可以仅对每个循环进行硬编码,因为只有4个。这是一个示例:

 //this passes the text input as an object to the list array var contactList = { list: [], addNew: function() { this.list.push({ firstName: document.getElementById('firstname').value, lastName: document.getElementById('lastname').value, email: document.getElementById('emailAdd').value, phoneNumber: document.getElementById('phoneNumber').value }); }, }; // this runs the addNew() function and clears the input fields afterwards var handlers = { addContact: function() { contactList.addNew(); document.getElementById('firstname').value = ''; document.getElementById('lastname').value = ''; document.getElementById('emailAdd').value = ''; document.getElementById('phoneNumber').value = ''; view.displayContact(); }, }; //this is where i'm trying to display the contacts array var view = { displayContact: function() { var contacts = document.getElementById('contactul'); while(contacts.firstChild ){ contacts.removeChild(contacts.firstChild ); } for (var i = 0; i < contactList.list.length; i++) { var liForFirstName = document.createElement('li'); contacts.appendChild(liForFirstName); liForFirstName.innerHTML += "First Name: " + contactList.list[i].firstName; var liForLastName = document.createElement('li'); contacts.appendChild(liForLastName); liForLastName.innerHTML += "Last Name: " + contactList.list[i].lastName; var liForEmail = document.createElement('li'); contacts.appendChild(liForEmail); liForEmail.innerHTML += "Email: " + contactList.list[i].email; var liForPhoneNumber = document.createElement('li'); contacts.appendChild(liForPhoneNumber); liForPhoneNumber.innerHTML += "Phone Number: " + contactList.list[i].phoneNumber; }; }, }; 
 <form> First name:<br> <input id="firstname" type="text" name="firstname"> <br> Last name:<br> <input id="lastname" type="text" name="lastname"> <br> Email Address:<br> <input id="emailAdd" type="text"> <br> Phone number:<br> <input id="phoneNumber" type="text"> <br> </form> <button onclick='handlers.addContact()'>Submit</button> <div id='displayContacts'> <ul id='contactul'> </ul> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置存储在数组中的对象的属性 - set properties of object stored in array 尝试从数组中的 object 中的数组中获取 select 属性。 抛出类型错误:无法读取未定义的属性 - Trying to select property from array within object, within array. Thrown TypeError: Cannot read properties of undefined JavaScript-通过在HTML5画布上单击存储在对象数组中的图像来获取对象的属性 - JavaScript- getting the properties of an object, by clicking an image stored in an array within the object, on the HTML5 canvas 尝试对对象数组进行排序以显示具有最高“得分”元素的前3个项目 - Trying to sort an object array to display the top 3 items with the highest “score” element 如何在流星上的HTML上将一个数组中的项目显示在另一个数组中? - How to display items in an array, within another array, onto HTML on meteor? 访问数组中的对象属性 - Accessing object properties within an array 数组中对象属性的总和 - Sum of object properties within an array 访问存储在javascript / jquery中数组中的对象的属性 - accessing properties of object stored in array in javascript/jquery 尝试显示存储在 localstorage 中的数组的值时,Object 可能是 null 错误(打字稿) - Object is possibly null error (typescript) when trying to display the value of an array which is stored in localstorage 显示本身位于 object 数组中的 object 的属性 - display the properties of an object that is itself in an object array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM