[英]Wrong result while trying to sum/rest values from different inputs
I'm trying to get a result (after sum/rest) from two different values, for example I have a Total var = 500
then I have two inputs... one for MXN and the other for USD, I also have a USD var = 17.5
what I want to do is that when I type a number on both inputs (MXN or USD) display a sum (for money received) and a rest (for change, in case that the received money were more than 500 or my Total value), at the moment I have this code, but just work for one input instead of both, how can I solve this? 我正在尝试从两个不同的值获取结果(总和/余数后),例如,我有
Total var = 500
那么我有two inputs... one for MXN and the other for USD, I also have a USD var = 17.5
我想做的是,当我在两个输入项(墨西哥比索或美元)上都输入数字时,显示一个总和(用于收到的钱)和一个余下的(用于找零,以防万一收到的钱超过500或总价值),目前我有此代码,但仅对一个输入(而不是两个)工作,我该如何解决呢?
Here is an working example 这是一个有效的例子
EDIT: 编辑:
Now I got both inputs working as expected but know I'm facing another issue... I have one label to show change value (when money received > total) and one to show when money received <= total. 现在,我的两个输入均按预期工作,但知道我面临另一个问题……我有一个标签来显示零钱价值(当收到的钱>总价时),并显示一个当收到的钱<=总价时。
For example... Total = 500, received = 450, my label need to show I need: $50, but if my total = 500, received = 600 I need to show: Change: $100 例如...总计= 500,已接收= 450,我的标签需要显示我需要:$ 50,但是如果我的总计= 500,已接收= 600,我需要显示:更改:$ 100
However here is my code: 但是这是我的代码:
$(document).ready(function() { var dolar = 17.50; $('#Total').on('keyup', function() { var valor = $(this).val(); if (valor == "") { $('#TotalLbl').text("$00.00"); } else { $('#TotalLbl').text("Total: $" + parseFloat(valor).toFixed(2)); } }); $('#MXN').on('keyup', function() { var valor = $(this).val(); if (valor == "") { $('#Received').text("$00.00"); } else { $('#Received').text("Received: $" + parseFloat(valor).toFixed(2)); var falta = ($('#Total').val() - valor); $('#INeedThisTo0').text("Change: $" + parseFloat(falta).toFixed(2)); } }); $('#USD').on('keyup', function() { var valor = $(this).val() * dolar; if (valor == "") { $('#Received').text("$00.00"); } else { $('#Received').text("Received: $" + parseFloat(valor).toFixed(2)); var falta = ($('#Total').val() - valor); $('#INeedThisTo0').text("Change: $" + parseFloat(falta).toFixed(2)); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input placeholder="Total.." type="number" id="Total" /> <input placeholder="MXN.." type="number" id="MXN" /> <input placeholder="USD.." type="number" id="USD" /> <br> <label id="TotalLbl"></label> <label id="Received"></label> <label id="INeedThisTo0"></label> <label id="Change"></label>
$('#USD').on('keyup', function() {
var valor =( $(this).val() * dolar)+$('MXN').val();
$('#MXN').on('keyup', function() {
var valor = $(this).val()+($('USD').val()*dolar);
Convert the valor value in both function as this 以此转换两个函数中的valor值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.