1
I cannot add the values and put the mask correctly between the inputs.
For example, I am adding the following values to each input:
- Input from Membership Value:
1.300,44
- Input 1 of Dependent Values:
1.300,44
- Input 2 of Dependent Values:
80,80
And in the input of Total Value will be placed the sum of the value of all inputs together.
With this current code, if I put 80,79
in Input 2 of Dependent Values, the value matches, but 80,80
does not hit. I do not know how to solve it.
Script of Membership Value:
function mascara(o, f) {
v_obj = o
v_fun = f
setTimeout("execmascara()", 1)
}
function execmascara() {
v_obj.value = v_fun(v_obj.value)
}
function mreais(v) {
v = v.replace(/\D/g, "") //Remove tudo o que não é dígito
v = v.replace(/(\d{2})$/, ",$1") //Coloca a virgula
v = v.replace(/(\d+)(\d{3},\d{2})$/g, "$1.$2") //Coloca o primeiro ponto
return v
}
Input from Membership Value:
<input type="text" id="txt1" name="adesao" class="form-control calcular" placeholder="R$" onkeypress="mascara(this,mreais)" onkeyup="calcular()">
Script of Total Value:
<script type="text/javascript">
function calcular() {
var soma = $('.calcular').get().reduce(function(soma, el) {
return (parseFloat(el.value.replace(/\./g, "").replace(",", "."), 10) || 0) + soma;
}, 0);
document.getElementById('result').value = soma;
mascara(document.getElementById('result'), mreais);
}
</script>
Input from Total Value:
<input type="text" class="form-control" name="total" id="result" readonly>
Script of Dependent Values:
<script type="text/javascript">
var AddTableRow = function(el) {
var tbody = $(el).closest('table').find('tbody');
var row = tbody.find('tr:last').clone();
var name = row.find('.calcular').attr('name');
var index = parseInt(name.match(/usuarios\[(\d+)\]\[valordependente\]/)[1], 10) + 1;
row.find('[name^="usuarios["]').each(function() {
if (this.name) {
this.name = this.name.replace(/^usuarios\[\d+\]/, "usuarios[" + index + "]");
}
});
tbody.append(row);
};
</script>
Here is a link in English about
Floating-Number
for those who want to know more about it. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html– relaxeaza
Thanks @Rafael
– Mauro Santos