简体   繁体   English

当用户单击按钮时,如何解构只有数据的数组?

[英]how can i destructure an array that only has data when the user clicks the button?

I need to destructure my array "persons" to show, for example, the first person entered by the user, but i don´t know how to do it if it doesn´t have objects until the user click the calculate button.我需要解构我的数组“persons”以显示,例如,用户输入的第一个人,但如果它没有对象,我不知道该怎么做,直到用户单击计算按钮。

Here's my code:这是我的代码:

 const persons = [] let nameHTMLelemento = document.getElementById("name") let amountHTMLelemento = document.getElementById("amount") let totalHTMLelemento = document.getElementById("total") let personsListHTMLelemento = document.getElementById("each-person") let splitHTMLelemento = document.getElementById("split") document.getElementById("calculate").addEventListener("click", enterPerson) function enterPerson() { let person = nameHTMLelemento.value; let amount = amountHTMLelemento.value; persons.push({ name: person, amount: parseFloat(amount), }); nameHTMLelemento.value = ""; amountHTMLelemento.value = ""; defineTotal(); } function defineTotal() { let list = ""; let total = 0; for (let i = 0; i < persons.length; i++) { total += persons[i].amount list += `${persons[i].name}: ${persons[i].amount} <br>`; } let iva = 1.21 totalHTMLelemento.innerHTML = total * iva; personsListHTMLelemento.innerHTML = list; splitHTMLelemento.innerHTML = (total * iva) / persons.length; }
 <div class="card-container"> <h1>splitter bill</h1> <p>enter the name of the person and his amount (21% of iva included):</p> <label id="name-text">Nombre</label> <br> <input type="text" id="name"> <br> <label id="monto-texto">amount</label> <br> <input type="number" id="amount"> <br> <button id="calculate">Ingresar</button> <p>Total: <span id="total"></span> </p> <div id="each-person"> </div> <p> Cada uno le toca aportar: <span id="split"></span> </p> </div> <script src="main.js"></script>

The most basic validation would be to simply check if person is empty or if parseFloat(amount) is NaN and return early if either is true.最基本的验证是简单地检查person是否为空或parseFloat(amount)是否为NaN ,如果其中任何一个为真,则提前返回。

function enterPerson() {

  let person = nameHTMLelemento.value;
  let amount = amountHTMLelemento.value;

  if (person === '' || isNaN(parseFloat(amount))) {
    return false;
  }

  persons.push({
    name: person,
    amount: parseFloat(amount),
  });

  // ...
}

 const persons = [] let nameHTMLelemento = document.getElementById("name") let amountHTMLelemento = document.getElementById("amount") let totalHTMLelemento = document.getElementById("total") let personsListHTMLelemento = document.getElementById("each-person") let splitHTMLelemento = document.getElementById("split") document.getElementById("calculate").addEventListener("click", enterPerson) function enterPerson() { let person = nameHTMLelemento.value; let amount = amountHTMLelemento.value; if (person === '' || isNaN(parseFloat(amount))) { return false; } persons.push({ name: person, amount: parseFloat(amount), }); nameHTMLelemento.value = ""; amountHTMLelemento.value = ""; defineTotal(); } function defineTotal() { let list = ""; let total = 0; for (let i = 0; i < persons.length; i++) { total += persons[i].amount list += `${persons[i].name}: ${persons[i].amount} <br>`; } let iva = 1.21 totalHTMLelemento.innerHTML = total * iva; personsListHTMLelemento.innerHTML = list; splitHTMLelemento.innerHTML = (total * iva) / persons.length; }
 <div class="card-container"> <h1>splitter bill</h1> <p>enter the name of the person and his amount (21% of iva included):</p> <label id="name-text">Nombre</label> <br> <input type="text" id="name"> <br> <label id="monto-texto">amount</label> <br> <input type="number" id="amount"> <br> <button id="calculate">Ingresar</button> <p>Total: <span id="total"></span> </p> <div id="each-person"> </div> <p> Cada uno le toca aportar: <span id="split"></span> </p> </div> <script src="main.js"></script>

This will get your snippet working, but you'll most likely want to build on this to provide feedback to the user indicating that a required field was left blank.这将使您的代码段正常工作,但您很可能希望以此为基础向用户提供反馈,表明必填字段留空。 There are plenty of duplicate questions on SO covering input validation. SO上有很多关于输入验证的重复问题。

暂无
暂无

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

相关问题 选择随机数组打印出来? 我怎样才能打印出包含图像在内的所有数据的随机数组? 当用户点击按钮时 - Selecting random array to print out! How can i print out random array with alle the data including the image? When user clicks on button 当用户单击按钮时,如何使用textarea弹出窗口? - How can I have a textarea popup when the user clicks a button? 用户单击按钮时如何执行JavaScript - How can i execute a javascript when a user clicks a button 如何解构此数组? - How can I destructure this array? 当数据动态来自数据库时,如何从对象数组中解构? - how can I destructure from an array of objects when data is coming from database dynamically? 如何循环遍历按钮数组,并且只有在用户单击数组中的下一个按钮时才继续循环 - How do I loop through an array of buttons and only continue looping if the user clicks the next button in the array 为什么当用户单击按钮时我无法显示我想要的数据 - why i can't show data i want when user clicks on button 如何在 JavaScript 中解构数组数组? - How can I destructure an array of arrays in JavaScript? 用户单击按钮时如何存储类名称,以便下次访问时可以将其用作默认名称? - How can I store a class name when a user clicks on a button so it can be used as a default on next visit? 当用户单击按钮时,如何 select 数组中的下一个项目? - How do I select the next item in an array when a user clicks on a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM