简体   繁体   English

如何使渲染列表显示来自用户输入的正确数据?

[英]How to make rendered list show correct data from user input?

I am creating a Budget app and I was able to get the user input to print out almost like a to-do app, but in this case, it's just the user's budget.我正在创建一个预算应用程序,我能够让用户输入几乎像待办事项应用程序一样打印出来,但在这种情况下,它只是用户的预算。

The issue I am having at the moment is that it's not printing out the user's input information.我目前遇到的问题是它没有打印出用户的输入信息。 This is what I have done.这就是我所做的。

  • Pushed render list into an array将渲染列表推送到数组中
  • render list is taking information from an object I created and is meant to print the information from the object, but it prints are duplicates.渲染列表从我创建的对象中获取信息,旨在打印来自对象的信息,但它打印的是重复的。

also for some reason it prints the syntax for the generated id syntax, which its not supposed to也出于某种原因,它会打印生成的 id 语法的语法,这是不应该的

 /*----Generate ID----*/ const createId = () => `${Math.floor(Math.random() * 10000)}$(new Date().getTime())}`; /*----Variable Objects----*/ const el = { list: document.querySelector(".list"), cashflow: document.querySelector(".cashflow"), catagory: document.querySelector(".catagory"), label: document.querySelector(".label"), startDate: document.querySelector(".start-date"), endDate: document.querySelector(".end-date"), number: document.querySelector(".end-date"), }; /*----Array with local Storage----*/ let budgetArray = []; /*----Budget list Object----*/ function makeNewBudget() { const data = { id: createId(), cashflowNew: el.cashflow.value, catagoryNew: el.catagory.value, labelNew: el.label.value, startDateNew: el.startDate.value, endDateNew: el.endDate.value, numberNew: el.number.value, }; return data; } /*----Render Budget List----*/ function renderList() { el.list.innerHTML = budgetArray.map(function(data, i) { return `<div class="entry"> <div class="list"> <button onclick="deleteItem(event, ${i})" class = "Archive" data-id="${data.id}"> <img src="../resources/Images/archive.png" alt="Archvie"> </button> <button <button onclick="editItem(event, ${i})" class = "Archive" data-id="${data.id}"> class = "edit" data-id="${data.id}"> <img src="../resources/Images/edit.png" alt="Edit"> </button> <div class="input" data-id="${data.id}"></div> <label class="print date">${data.startDateNew} - ${data.endDateNew} <label class="print income">${data.cashflowNew} <label class="print cata">${data.catagoryNew} <label class="print reason">${data.labelNew} <label class="print amount">${data.numberNew} </div> </div>`; }); el.input.value = ""; } /*----form validation----*/ let budgetButton = document.querySelector(".budget-button"); let label = document.querySelector(".label"); let startDate = document.querySelector(".start-date"); let endDate = document.querySelector(".end-date"); let num = document.querySelector(".number"); budgetButton.addEventListener("click", () => { if (!label.value || !startDate.value || !endDate.value || !num.value) { alert("please make sure all inputs are filled"); } budgetArray.push(makeNewBudget()) renderList(); storedEntry(); }); /*----Store Stored budget list----*/ function storedEntry() { window.localStorage.setItem(STORAGE_KEY, budgetArray); }
 <div class="create-budget"> <form class="budget"> <input class="budget-button" type="button" value="Create your budget"> <select class="catagory cashflow" name="income/expense" class="income/expense"> <option class="options" value="income">Income</option> <option class="options" value="expense">Expense</option> </select> <select name="Catagory" class="catagory" value="Catagory"> <option class="options" value="House Hold">House Hold</option> <option class="options" value="Car">Car</option> <option class="options" value="entertainment">Entertainment</option> <option class="options" value="investments">Investments</option> <option class="options" value="business">Business</option> <option class="options" value="savings">Savings</option> </select> <input class="label" type="text" placeholder="Example rent"> <input class="start-date" type="date"> <input class="end-date" type="date"> <input class="number" type="text" placeholder="0,0"> </form> </div> <div class="new-budet"> <div class="title"> <h5>Date</h5> <h5>Income/Expenses</h5> <h5>Catagory</h5> <h5>Items</h5> <h5>Amount</h5> </div> </div> <div class="list"></div> <!--New Entry for user <div class="entry"> <div class="list"> <button class = "Archive"> <img src="../resources/Images/archive.png" alt="Archvie"> </button> <button class = "edit"> <img src="../resources/Images/edit.png" alt="Edit"> </button> <label class="input" data-id="id"> <label class="print">date <label class="print income">income <label class="print cata">catagory <label class="print reason">Reason <label class="print amount">amount </div> </div> --> <div class="total"></div>

I fixed the issue, it seems as though there was a duplicate in the HTML element and in the el object for the js side.我解决了这个问题,似乎 HTML 元素和 js 端的 el 对象中有重复项。

old HTML旧的 HTML

<select class="catagory cashflow" name="income/expense" class="income/expense">
<select name="Catagory" class="catagory" value="Catagory">

new HTML新的 HTML

<select id="cashflow" name="income/expense" class="income/expense">
<select name="Catagory" class="catagory" value="Catagory">

old el object旧的 el 对象

const el = {
number: document.querySelector(".end-date"),
};

new el object新的 el 对象

const el = {
number: document.querySelector(".number"),

暂无
暂无

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

相关问题 这是从获取请求中在React中显示数据的正确方法,如果不是,我如何更改呈现给组件的JSX? - Is this the correct way to show data in React from a fetch request, if not how do I change JSX being rendered to a component? 仅当您输入正确的单词时如何使用户重定向<input> - How to make user redirect only if you type in correct word <input> 如何从用户输入字段中获取数据并在不提交的情况下向用户显示? - How can I get the data from user input fields and show user back without submission? 如何制作一个无数据的渲染组件,以后再用数据渲染? - How to make a react component that can be rendered with no data and later rendered with data? 如何将 div 附加到渲染列表中的输入值? - How to attach a div to input value in rendered list? 如何使用 javascript 创建一个 function,它将检查字段值/用户输入并在输入正确时显示警报? - How to create a function using javascript which will check a field value/user input and show alert if the input is correct? 动态字段自动完成-显示来自mysql的用户输入数据 - autocomplete on dynamic fields - show user sugestions from mysql for input data 如何在 HTML 中对用户输入文本框进行编码,并使用 javascript 如果答案正确则显示解释,否则说不正确 - How to code a user input textbox in HTML and use javascript to show an explanation if the answer is correct otherwise say incorrect 如何使用AngularJS以正确的方式显示Json文件中的数据 - how to show data from Json file in correct way with AngularJS 如何根据提示输入数据制作数组? - How to make an array from prompt input data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM