0
I have a function
that you would like to receive the id values of some quantity, unit value and result fields.
But I’m not getting it.
`
String.prototype.formatMoney = function() {
var v = this;
if(v.indexOf('.') === -1) {
v = v.replace(/([\d]+)/, "$1,00");
}
v = v.replace(/([\d]+)\.([\d]{1})$/, "$1,$20");
v = v.replace(/([\d]+)\.([\d]{2})$/, "$1,$2");
return v;
};
function id( el ){
return document.getElementById( el );
}
function getMoney( el ){
var money = id( el ).value ? id( el ).value.replace( ',', '.' ) : 0;
return parseFloat( money )*100;
}
function multiplicacao(id1, id2, id3){
alert(id1, id2, id3);
var qtd = id(id1).value;
var unit = getMoney(id2);
var total = qtd*unit;
id(id3).value = String(total/100).formatMoney();
}
//mascara para campos de R$
function moeda(z){
v = z.value;
v=v.replace(/\D/g,"")
//permite digitar apenas números
v=v.replace(/[0-9]{12}/,"inválido")
//limita pra máximo 999.999.999,99
v=v.replace(/(\d{1})(\d{8})$/,"$1.$2")
//coloca ponto antes dos últimos 8 digitos
v=v.replace(/(\d{1})(\d{5})$/,"$1.$2")
//coloca ponto antes dos últimos 5 digitos
v=v.replace(/(\d{1})(\d{1,2})$/,"$1,$2")
//coloca virgula antes dos últimos 2 digitos
z.value = v; }
<html>
<head>
<script type="text/javascript" src="js/funcoes.js"></script>
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
</head>
<body>
<input id="campo1" onKeyUp='multiplicacao(this.id)' type='text' name='' class='form-control' size='10'/>
<input id="campo2" onKeyUp='moeda(this);multiplicacao(this.id)' type='text' name='' class='form-control' size='10'/>
<input id="campo4" onKeyUp='moeda(this);multiplicacao(this.id)' name="campo4" readonly="readonly" class='form-control' size='10'/>
</body>
</html>
`
You have a
onKeyUp
in an inputreadonly
... that will never be called.– Sergio
@Sergio took the onkeyup left so:
id('campo4').value = String(total/100).formatMoney();
and I took the third parameter of the function but it doesn’t work– Sarah