4
I need my code to update the Increment field when the value of the Total field is changed to a value greater than the value of the Sub.Total or update the Discount field when the Total field is smaller than the Sub.Total field with the value of the difference, and when a value is added in the Increase field or in the Discount field it updates the Total field.
In my code the field Total is already working correctly when the field Increase or Discount is updated, but I need to work the other way too, as I put above, IE, update also the field Increase and Discount when changed the field Total.
This is the HTML code
<div id="DivOcultaBaixa" class="" style="display: none;">
<div class="control-group span3">
<div class="control span4" for="focusedInput">Sub. Total:</div>
<input class="input-medium focused span6 calc" name="subtotal" id="subtotal" readonly="readonly" type="text" value="<?php echo number_format($soma_saldo, 2, ',', '.') ?>"/>
</div>
<div class="control-group span3">
<div class="control span4" for="focusedInput">Acréscimo:</div>
<input class="input-medium focused span6 calc" name="acrescimo" id="acrescimo" type="text" value=""/>
</div>
<div class="control-group span3">
<div class="control span4" for="focusedInput">Desconto:</div>
<input class="input-medium focused span6 calc" name="desconto" id="desconto" type="text" value=""/>
</div>
<div class="control-group span3">
<div class="control span4" for="focusedInput"><strong>TOTAL:</strong></div>
<input type="text" id="total_val" style="background: #FFCC99; font-weight: bold;" name="total_val" class="input-medium focused span6 result" readonly="readonly" value="<?php echo number_format($soma_saldo, 2, ',', '.') ?>"/>
</div>
<div class="control-group span3">
<div class="control span4" for="focusedInput">Valor Pago:</div>
<input class="input-medium focused span6 calc" name="pagoparc" id="pagoparc" type="text" value=""/>
</div>
<div class="control-group span3">
<div class="control span4" for="focusedInput"><strong>SALDO:</strong></div>
<input class="input-medium focused span6 calc" style="background: #FFCC99; font-weight: bold;" name="saldodev" id="saldodev" type="text" value="<?php echo number_format($soma_saldo, 2, ',', '.') ?>"/>
</div>
</div>
Script js
$(document).ready(function(){
//Quando o valor do campo mudar...
$('.calc').change(function(){
var subtotal = parseFloat($('#subtotal').val().replace(".", "").replace(",", ".")) || 0.00;
var acrescimo = parseFloat($('#acrescimo').val().replace(".", "").replace(",", ".")) || 0.00;
var desconto = parseFloat($('#desconto').val().replace(".", "").replace(",", ".")) || 0.00;
var pagoparc = parseFloat($('#pagoparc').val().replace(".", "").replace(",", ".")) || 0.00;
//O total
var total = subtotal + acrescimo - desconto;
var saldo = total - pagoparc;
$('#total_val').val(number_format(total,2, ',', '.'));
$('#saldodev').val(number_format(saldo,2, ',', '.'));
});
});
function number_format( number, decimals, dec_point, thousands_sep ) {
var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
var d = dec_point == undefined ? "," : dec_point;
var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");}
What have you tried?
– Renato Gama
I tried using the same logic and also if (if the total value is different from the sub. total value), I was unsuccessful.
– Irio Broleis Filho
Can you put the missing code here? For example what you do
number_format()
?– Sergio
I added number_format() code
– Irio Broleis Filho
@Is this what you want? http://jsfiddle.net/pfok6a4s/
– Sergio
For security reasons, I suggest you do the calculations by PHP and return the results via AJAX by the method
$.post()
. Keep doing so many negotiations by javascript is not very smart, anyone can change their values.– Ivan Ferrer