简体   繁体   English

如何将旧输入值添加到新输入值?

[英]How can I add old input value to new input value?

 let currencySymbol = '$'; document.querySelector('.pay').addEventListener('click', (e) => { e.preventDefault(); // Get input cash received field value, set to number let amount = document.querySelector('.received').value; amount *= 1; // Set cashReturn to return value of pay() let cashReturn = pay(amount); let paymentSummary = document.querySelector('.pay-summary'); let div = document.createElement('div'); // If total cash received is greater than cart total thank customer // Else request additional funds if (cashReturn >= 0) { div.innerHTML = ` <p>Cash Received: ${currencySymbol}${amount}</p> <p>Cash Returned: ${currencySymbol}${cashReturn}</p> <p>Thank you;</p> `. } else { // reset cash field for next entry document.querySelector('.received');value = ''. div:innerHTML = ` <p>Cash Received: ${currencySymbol}${amount}</p> <p>Remaining Balance. ${cashReturn}$</p> <p>Please pay additional amount;</p> <hr/> `. } paymentSummary;append(div); }); let totalPaid = 0; function pay(totalPaid) { let cartSum = 50; //using a dummy value for snippet return totalPaid - cartSum; }
 .checkout-container { max-width: 34em; padding: 2em; background: #efefef; }
 <div class="checkout-container"> <h2>Checkout</h2> <div class="checkout"> <div class="cart-total"></div> <form> <label>Enter Cash Received:</label> <input class="received" type="text"> <button class="pay">Submit</button> </form> <h3>Receipt</h3> <div class="pay-summary"></div> </div> </div>

I'm implementing a simple cash register where user enters how much they paid into an input field, and it will subtract it from the total cost of the items.我正在实现一个简单的收银机,用户在其中输入他们在输入字段中支付的金额,然后它将从项目的总成本中减去。 If the number returned is positive, it shows that the customer wil be given back the remaining change.如果返回的数字是正数,则表明将把剩余的零钱退还给客户。 If it's a negative value, the customer needs to add more money into the input field, which should be summed with their previous input.如果它是一个负值,客户需要在输入字段中添加更多的钱,这应该与他们之前的输入相加。

Right now, the previous value is not being added to the new value:现在,以前的值没有被添加到新值中: 在此处输入图像描述

After I input the remaining $15, there should be 0 remaining balance.输入剩余的15美元后,余额应该为0。

If you mean to type an amount of cash received more than once, you need to keep track of the amount of money received so far.如果您打算多次输入收到的现金数额,则需要跟踪到目前为止收到的现金数额。

There are multiple ways to achieve that, here I opted for keeping track of it inside the value of an added input element.有多种方法可以实现这一点,在这里我选择在添加的输入元素的值内跟踪它。

In my demo the function cartTotal() always returns 78.45 as the amount to pay, and to reset the amount of money received so far, you just need to click the reset button so that the corresponding input value will be set back to zero.在我的演示中,function cartTotal()总是返回78.45作为支付金额,要重置目前收到的金额,您只需点击重置按钮,相应的输入值将被设置回零。

 function pay(totalPaid){ let cartSum = cartTotal(); return totalPaid - cartSum; } //Arbitrary number function cartTotal(){ return 78.45; } function resetGame(){ document.getElementById('sofar').value = 0; document.getElementById('received').value = 0; document.getElementById('cashreturn').value = 0; } document.querySelector('.pay').addEventListener('click', (e) => { e.preventDefault(); // Get input cash received field value, set to number const amount = parseFloat( document.getElementById('received').value ); //**Here updates the amount received so far const receivedSoFar = parseFloat( document.getElementById('sofar').value ); document.getElementById('sofar').value = receivedSoFar + amount; // Set cashReturn to return value of pay() const cashReturn = pay(amount + receivedSoFar); document.getElementById('cashreturn').value = cashReturn.toFixed(2); });
 body{ font-family: sans-serif; } input, button{ padding: .2rem; width: 5rem; font-size: 15px; } input[disabled]{ outline: none; border: none; font-size: 20px; } button{ cursor: pointer; }
 <label>Enter cash:</label> <input type="text" id="received"> <button class="pay">PAY</button> <hr> <label>Received so far:</label> <input type="text" id="sofar" readonly disabled value="0"> <br> <label>Cash return:</label> <input type="text" id="cashreturn" readonly disabled value="0"> <br> <button onclick="resetGame();">RESET</button>

Your amount variable only represents the last input.您的amount变量仅代表最后一次输入。 Any previous submitted amounts are lost.之前提交的任何金额都将丢失。 To fix this, define amount as a global variable, and add to that what the user has entered.要解决此问题,请将amount定义为全局变量,并将用户输入的内容添加到该变量中。

So change this part:所以改变这部分:

document.querySelector('.pay').addEventListener('click', (e) => {
  e.preventDefault();

  // Get input cash received field value, set to number
  let amount = document.querySelector('.received').value;
  amount *= 1;

to:到:

let amount = 0; // <--- define here so to accumulate paid amounts
document.querySelector('.pay').addEventListener('click', (e) => {
  e.preventDefault();

  // Get input cash received field value, set to number
  let paid = document.querySelector('.received').value;
  amount += +paid; // <--- accumulate

Your adapted snippet:您改编的代码段:

 let currencySymbol = '$'; let amount = 0; // <--- define here so to accumulate paid amounts document.querySelector('.pay').addEventListener('click', (e) => { e.preventDefault(); // Get input cash received field value, set to number let paid = document.querySelector('.received').value; amount += +paid; // <--- accumulate // Set cashReturn to return value of pay() let cashReturn = pay(amount); let paymentSummary = document.querySelector('.pay-summary'); let div = document.createElement('div'); // If total cash received is greater than cart total thank customer // Else request additional funds if (cashReturn >= 0) { div.innerHTML = ` <p>Cash Received: ${currencySymbol}${amount}</p> <p>Cash Returned: ${currencySymbol}${cashReturn}</p> <p>Thank you;</p> `. } else { // reset cash field for next entry document.querySelector('.received');value = ''. div:innerHTML = ` <p>Cash Received: ${currencySymbol}${amount}</p> <p>Remaining Balance. ${cashReturn}$</p> <p>Please pay additional amount;</p> <hr/> `. } paymentSummary;append(div); }); let totalPaid = 0; function pay(totalPaid) { let cartSum = 50; //using a dummy value for snippet return totalPaid - cartSum; }
 .checkout-container { max-width: 34em; padding: 2em; background: #efefef; }
 <div class="checkout-container"> <h2>Checkout</h2> <div class="checkout"> <div class="cart-total"></div> <form> <label>Enter Cash Received:</label> <input class="received" type="text"> <button class="pay">Submit</button> </form> <h3>Receipt</h3> <div class="pay-summary"></div> </div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM