1
I’m having trouble understanding how input calculation works with Jquey.
I will show the following example:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="text" name="campo_1" class="campo_1">
<input type="text" name="campo_2" class="campo_2">
<input type="text" name="res_1" class="res_1">
<!-- Resultado de campo_1 * campo_2 -->
<input type="text" name="campo_3" class="campo_3">
<input type="text" name="campo_4" class="campo_4">
<input type="text" name="res_2" class="res2">
<!-- Resultado de campo_3 * campo_4 -->
<input type="text" name="res_3" class="res3">
<!-- Resultado de campo_1 + campo_2 + campo_3 + campo_4 -->
</body>
</html>
I don’t know, I have no idea how to do that when you put values in the fields, you get the result.
I’ve made adaptations in other projects like:
<script type="text/javascript">
function updateTotal_836() {
var total_836 = 0;
var list_836 = document.getElementsByClassName("input[836]");
var values = [];
for(var i = 0; i < list_836.length; ++i) {
values.push(parseFloat(list_836[i].value));
}
total_836 = values.reduce(function(previousValue, currentValue, index, array){
return previousValue * currentValue;
});
if(isNaN(parseFloat(total_836))){
document.getElementById("valor_total[836]").value = 0;
}else{
document.getElementById("valor_total[836]").value = total_836;
}
}
</script>
But I don’t even understand how it starts and more or less how it ends. I’m getting deeper into Jquery, but I admit I need help this time.
Thank you.
It worked, but a problem has arisen.
I made that calculation:
$('input[name="rec_vista"]').change(function(e) {
if (!$('input[name="rec_vista"]').val()) {
$('input[name="rec_vista"]').val(0);
}
var rec_vista = parseFloat($('input[name="rec_vista"]').val());
a_vista = rec_vista + 10;
$('input[name="a_vista"]').val(a_vista);
});
If I put 1,000,000 into the input, instead of calculating 1,010,00, it calculates 11.
I put, I took parentheses, but nothing to improve this crazy sum.
If for the last time anyone can give me a light, I’d appreciate it.