Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Boris

Devo mais do que tenho

Recommended Posts

Galera é o seguinte, estou com o seguinte problema

 

eu tenho quatro campos

 

campo1 = Tenho

campo2 = Devo

campo3 = Sobra

campo4 = Falta

 

eu peguei um script no forum e complementei da maneira que eu precisava mas como ele é grande pra colocar eu procurei deixar ele bem simples de maneira que alguem possa me ajudar....

 

Eu na realidade preciso do seguinte:

 

quando coloco em tenho: 50,00

e quando coloco em devo: 60,00

então em falta tem que aparecer : 10,00

 

quando coloco em devo: 60,00

e quando coloco em tenho: 70,00

então em sobra tem que aparecer : 10,00

 

<script type="text/javascript">

addEvent = function(o, e, f, s) {

var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;

r[r.length] = [f, s || o], o[e] = function(e){

try{

(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});

e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});

e.target || (e.target = e.srcElement || null);

e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;

}catch(f){}

for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));

return e = null, !!d;

}

};

 

function formatCurrency(o, n, dig, dec) {

o.c = !isNaN(n) ? Math.abs(n) : 2;

o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;

addEvent(o, "keypress", function(e) {

if (e.key > 47 && e.key < 58) {

var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;

if(o.maxLength + 1 && l >= o.maxLength) return false;

l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);

for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);

n && n < l && (s[l - ++n] += o.dec);

o.value = s.join("");

}

e.key > 30 && e.preventDefault();

});

}

 

function fmtMoney(n, c, d, t){

var m = (c = Math.abs© + 1 ? c : 2, d = d || ",", t = t || ".",

/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;

return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,

"$1" + t) + (c ? d + (+m[2] || 0).toFixed©.substr(2) : "");

};

 

function soma() {

var f = document.forms[0];

var valor1 = parseFloat(f.campo1.value.replace('.','').replace(',','.'));

var valor2 = parseFloat(f.campo2.value.replace('.','').replace(',','.'));

var valor3 = ('' + ((valor1||0) - (valor2||0)));

f.campo3.value = fmtMoney(valor3);

}

 

function init() {

formatCurrency(document.forms[0].campo1, 2, ".", ","); <!-- O NUMERO DOI É A QUANTIDADE DE CASAS DECIMAIS DEPOIS DO PONTO -->

formatCurrency(document.forms[0].campo2, 2, ".", ",");

}

 

window.onload = init;

</script>

 

<form>

<table width="300" border="0" cellspacing="0" cellpadding="0">

<tr>

<td>Tenho</td>

<td><input name="campo1" onkeyup="soma()" size="20" /></td>

</tr>

<tr>

<td>Devo</td>

<td><input name="campo2" onkeyup="soma()" size="20" /></td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> Sobra </td>

<td><input name="campo3" size="20" /></td>

</tr>

<tr>

<td>Falta</td>

<td><input name="campo4" id="cam'po4" size="20" /></td>

</tr>

</table>

</form>

 

Valew

Compartilhar este post


Link para o post
Compartilhar em outros sites

Seria mais facil, mas se tratando dos usuarios que eu lido em especifico eles não acionariam o botão ou esqueceriam do mesmo, mas se você tiver o codigo me passa por favor pois posso ver em cima do codigo e ter alguma ideia de como fazer;

 

 

ou se tiver alguem que possa me ajudar com o mesmo eu agradeço pessoal.

 

 

Valew AgugustoClaro.

 

Abs

Compartilhar este post


Link para o post
Compartilhar em outros sites

<html>
<head>
<script>
function func() {
tenho = document.getElementById("tenho").value;
devo = document.getElementById("devo").value;
if (devo > tenho) {
document.getElementById("sobra").value = tenho - devo;
document.getElementById("falta").value = "";
}else if (tenho > devo) {
document.getElementById("falta").value = devo - tenho;
document.getElementById("sobra").value = "";
}else{
document.getElementById("sobra").value = 0;
document.getElementById("falta").value = 0;
}
}
</script>
</head>
<body>
Tenho: <input id="tenho"><br>
Devo: <input id="devo"><br>
Sobra: <input id="sobra"><br>
Falta: <input id="falta"><br>
<input type="button" value="Testar" onclick="func();">
</body>
</html>

Tá ai! Espero ter ajudado...

^^

Compartilhar este post


Link para o post
Compartilhar em outros sites

Não está dando certo....

 

Eu tentei fazer da seguinte forma....

 

Logicamente estaria certo mas não estou conseguindo fazer que ele apareça no campo alguem pode me ajudar ...

 

 

<script type="text/javascript">

addEvent = function(o, e, f, s) {

var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;

r[r.length] = [f, s || o], o[e] = function(e){

try{

(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});

e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});

e.target || (e.target = e.srcElement || null);

e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;

}catch(f){}

for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));

return e = null, !!d;

}

};

 

function formatCurrency(o, n, dig, dec) {

o.c = !isNaN(n) ? Math.abs(n) : 2;

o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;

addEvent(o, "keypress", function(e) {

if (e.key > 47 && e.key < 58) {

var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;

if(o.maxLength + 1 && l >= o.maxLength) return false;

l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);

for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);

n && n < l && (s[l - ++n] += o.dec);

o.value = s.join("");

}

e.key > 30 && e.preventDefault();

});

}

 

function fmtMoney(n, c, d, t){

var m = (c = Math.abs© + 1 ? c : 2, d = d || ",", t = t || ".",

/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;

return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,

"$1" + t) + (c ? d + (+m[2] || 0).toFixed©.substr(2) : "");

};

 

function soma() {

var f = document.forms[0];

var valor1 = parseFloat(f.campo1.value.replace('.','').replace(',','.'));

var valor2 = parseFloat(f.campo2.value.replace('.','').replace(',','.'));

var valor3 = ('' + ((valor1||0) - (valor2||0)));

 

if(valor1 >= valor2 {

 

f.campo3.value = fmtMoney(valor3);

 

}

 

if(valor1 < valor2){

 

f.campo4.value = fmtMoney(valor3);

}

 

 

}

 

 

function init() {

formatCurrency(document.forms[0].campo1, 2, ".", ","); <!-- O NUMERO DOI É A QUANTIDADE DE CASAS DECIMAIS DEPOIS DO PONTO -->

formatCurrency(document.forms[0].campo2, 2, ".", ",");

}

 

window.onload = init;

 

</script>

 

<form name="formulario">

<table width="300" border="0" cellspacing="0" cellpadding="0">

<tr>

<td>Tenho</td>

<td><input name="campo1" onkeyup="soma()" size="20" /></td>

</tr>

<tr>

<td>Devo</td>

<td><input name="campo2" onkeyup="soma()" size="20" /></td>

</tr>

<tr>

<td> </td>

<td> </td>

</tr>

<tr>

<td> Sobra </td>

<td><input name="campo3" size="20" /></td>

</tr>

<tr>

<td>Falta</td>

<td><input name="campo4" id="cam'po4" size="20" /></td>

</tr>

</table>

</form>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.