javascript - 按下按钮后循环遍历对象数组
问题描述
我正在尝试遍历对象数组,以根据我的库项目的输入值在网格上显示它们。我的循环代码是
const addBook = (ev) => {
ev.preventDefault();
let myLibrary = [];
let bookInfo = {
title: document.getElementById('title').value,
author: document.getElementById('author').value,
pages: document.getElementById('pages').value,
}
myLibrary.push(bookInfo)
for (i = 0; i < myLibrary.length; i++) {
console.log(myLibrary)
var container = document.getElementById('book-shelf')
var div = document.createElement('div')
div.classList.add('cell')
container.appendChild(div);
}
var submitBtn = document.querySelector('.submit-btn');
submitBtn.addEventListener('click', addBook)
每次我输入标题、作者和页面值并单击提交按钮时,它都会通过并给我一个单元格。如果尝试添加另一本书,它给了我 3 个单元格而不是 2 个。再添加一个单元格给我 6 个而不是 3 个。我怎样才能让它每次都可以添加一本书而不是多次添加?
解决方案
主要有三个问题。
在函数之后和按钮侦听器之前缺少大括号。
myLibrary
每次调用函数时都会重新定义,这是您每次都必须查看数据的原因之一。您想在函数外部定义它,以便在addBook
调用时一次添加一本书。不再
myLibrary
需要每次都重新定义循环。我们可以将这本书的 HTML 添加到书架上onSubmit
。
(注意:在这个工作示例中(我为表格、输入和按钮添加了一些 HTML,并创建了一些代码来将新书添加到书架上),我已将myLibrary
变量重命名为bookShelf
以保持一致HTML 命名。)
// Cache all the elements up front
const titleEl = document.getElementById('title');
const authorEl = document.getElementById('author');
const pagesEl = document.getElementById('pages');
const bookshelfEl = document.getElementById('bookshelf');
const submitBtn = document.querySelector('.submit-btn');
// Add the listener
submitBtn.addEventListener('click', addBook, false);
titleEl.focus();
// Our bookShelf variable is now
// outside the function
const bookShelf = [];
function addBook() {
// Because we've cached the elements
// we can now just grab the values from each
const bookInfo = {
title: titleEl.value,
author: authorEl.value,
pages: pagesEl.value,
}
bookShelf.push(bookInfo);
// Once we've added our book we can grab the
// title, author, and pages variables from it
const { title, author, pages } = bookInfo;
// Create a row for the table
const row = document.createElement('tr')
// Create some HTML and add it to the div
row.innerHTML = `
<td>${title}</td>
<td>${author}</td>
<td>${pages}</td>
`;
bookshelfEl.appendChild(row);
}
table { border-collapse: collapse; border: 2px solid #565656; width: 100%; }
td { text-align: center; }
.heading { background-color: #efefef; border-top: solid thin #565656; }
tr { border: solid thin #ababab; }
<input placeholder="Title" id="title" />
<input placeholder="Author" id="author" />
<input placeholder="Pages" id="pages" />
<button type="button" class="submit-btn">Submit</button>
<H3>Book shelf</H3>
<table>
<tbody id="bookshelf">
<tr class="heading">
<td>Title</td>
<td>Author</td>
<td>Pages</td>
</tr>
</tbody>
</table>
附加文件
推荐阅读
- typescript - 类装饰器上的打字稿文档-返回“类扩展构造函数{}”的函数
- python - 在 Python 3 中执行 bash 命令
- html - 如何使用网格引导导航?
- javascript - 如何仅使用纯 Javascript 代码从 HTML 页面上的目录中列出文件
- maven - Netbeans 11 - Maven 和 JavaFX - javafx.application 不存在
- php - 我需要帮助使提交按钮仅在您在文本框中输入时出现
- c# - 将 LINQ-to-JSON 查询从 c# 转换为 VB.NET 后如何解决 InvalidCastException?
- javascript - 成功地将`this`上下文传递给异步函数但是如何在NodeJS完成后检索`this`上下文?
- javascript - 使用 NodeJS 从 AJAX 调用中搜索 MongoDB
- python - 如何在没有片段着色器的情况下在 OpenGL 中实现阈值效果?