Your HTML must be changed. Add property onkeyup
to the fields encargos
and desconto
.
Custo
<input type='text' class='form_campos calc' $input_total id='total_pedido' name='total_pedido' readonly='true' value='5.000,35' id="total_pedido">
<br>
Frete
<input type='text' class='form_campos calc' id='encargos' name='encargos' onkeyup='calcular()'>
<br>
Desconto
<input type='text' class='form_campos calc' id='desconto' name='desconto' onkeyup='calcular()'>
<br>
Total
<input type='text' class='form_campos calc2' id='total_val' name='total_val'>
Next we need to define the function calcular
:
//É necessário verificar se o campo "total_pedido" já foi renderizado e pode ser referenciado pelo script,
//por isso é preciso escutar o evento `DOMContentLoaded`.
var total = 0;
document.addEventListener("DOMContentLoaded", function(event) {
total = document.getElementById("total_pedido").value;
//Retiramos a formatação do campo e convertemos em uma string
total = Number(total.replace(/[.]+/g,"").replace(",","."));
//A função é executada para preencher o campo com o valor total
calcular();
});
function calcular() {
//Obtemos o valor do encargo e desconto
var encargos = Number(document.getElementById("encargos").value);
var desconto = Number(document.getElementById("desconto").value);
var t = total + encargos - desconto;
//Atualizamos o campo "total_val" com o valor total
document.getElementById("total_val").value = t.toLocaleString('pt-BR');
}
See working here: https://jsfiddle.net/kmjr5atg/9/
There’s only two other things. I noticed that to run I have to put a value on the freight or discount, have to run even if they are blank? ie already load the page running. 2º and possible to write the total value in the default 1.000,00?
– Hugo Borges
@Hugoborges, yes, has. I changed the answer, see if it is the intended.
– Filipe Moraes
@Hugoborges added the formatted value in the reply example, see how it works: https://jsfiddle.net/kmjr5atg/4/
– Filipe Moraes
that’s right, but instead of appearing the R$ in total I need the value only formatted in en 1.000,00.
– Hugo Borges
@Hugoborges just remove the string "RS" in the above code. I updated the answer.
– Filipe Moraes
but it doesn’t get formatted in 1,000.00 standard
– Hugo Borges
@Hugoborges just change the line where we have
document.getElementById("total_val").value = t;
fordocument.getElementById("total_val").value = t.toLocaleString();
. See, I posted in response this option.– Filipe Moraes
friend forgive my ignorance hahahah but it did not work with me. The number of the total is like this: 5022.35 and I need it to stay like this: 5.022,35
– Hugo Borges
@Hugoborges but it’s like this, review the answer I gave and see here the code I posted above working: https://jsfiddle.net/kmjr5atg/9/
– Filipe Moraes
Strange, here really isn’t working. Take a look at this print I took. http://s11.postimg.org/ykhnwg6qb/Captura_de_Tela_2016_02_24_s_13_14_19.png. Note that the valar is not formatted with . and ,
– Hugo Borges
friend noticed that the error occurs only in safari, strange. But anyway thank you very much for the solution. Just one more question, it is possible to make that when changing the total_val the system informs the discount?
– Hugo Borges
@Hugoborges has yes, see here it works: https://jsfiddle.net/kmjr5atg/11/
– Filipe Moraes
very good, now has to pass the discount to %. there will be everything 100% :)
– Hugo Borges