Ir para conteúdo

POWERED BY:

Arquivado

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

viniciusjau

[Resolvido] Somar valores e formatar campos

Recommended Posts

Fiz um formulário onde o usuario vai digitar a quantidade de um produto e ele irá somar o valor total baseado no valor unitário.

 

Agora tentei um incluir um campo no final que irá somar os valores totais de todos os procutos... Esse campo está somando, mas não está formatandoo campo o valor fica assim por exemplo: 15.190000 e o correto seria ficar 15.20

 

Meu codigo:

<script>

function soma1(){

sa = document.getElementById("qtde1").value;

sb = document.getElementById("vlr1").value;

rsa = sa*sb;

vss = rsa.toFixed(2);

document.getElementById("total1").value = vss;

}

 

function soma2(){

sa = document.getElementById("qtde2").value;

sb = document.getElementById("vlr2").value;

rsa = sa*sb;

vss = rsa.toFixed(2);

document.getElementById("total2").value = vss;

}

 

function somatotal()

{

somatudo = (form.total1.value*1) + (form.total2.value*1);

somatudogeral = somatudo.toFixed(2);

document.getElementById("totalgeral").value = somatudo;

}

</script>

 

E o html

<form name="form">

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

<tr>

<td width="150" height="50"><strong>Quantidade:</strong><br />

<input name="qtde1" type="text" class="campoform1" id="qtde1" style="height:20px; width:80px;" onkeyup="soma1();somatotal();" value="2" /></td>

<td width="210"><strong>Valor Unitario:</strong><br />

<input name="vlr1" type="text" class="campoform1" id="vlr1" style="height:20px; width:110px;" value="2.00" disabled="disabled" /></td>

<td width="140"><strong>Total:</strong><br />

<input name="total1" type="text" class="campoform1" id="total1" style="height:20px; width:80px;" value="4.00" /></td>

</tr>

<tr>

<td height="40"><input name="qtde2" type="text" class="campoform1" id="qtde2" style="height:20px; width:80px;" onkeyup="soma2();somatotal();" value="1" /></td>

<td><input name="vlr2" type="text" class="campoform1" id="vlr2" style="height:20px; width:110px;" value="2.53" disabled="disabled" /></td>

<td><input name="total2" type="text" class="campoform1" id="total2" style="height:20px; width:80px;" value="2.53" /></td>

</tr>

<tr>

<td height="40"> </td>

<td> </td>

<td><input name="totalgeral" type="text" id="totalgeral" style="height:20px; width:80px;" value="6,53" /></td>

</tr>

</table>

</form>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olá.. obrigado pela ajuda...

Já tinha colocado no meu codigo o toPrecision(2) alterei para o toFixed(2)

 

Agora encontrei um erro e corrigi:

somatudo = (form.total1.value*1) + (form.total2.value*1);

somatudogeral = somatudo.toFixed(2);

document.getElementById("totalgeral").value = somatudo;

 

Alterei para:

somatudo = (form.total1.value*1) + (form.total2.value*1);

somatudogeral = somatudo.toFixed(2);

document.getElementById("totalgeral").value = somatudogeral;

 

Porém agora o codigo paou de funcionar no Firefoz... alguém tem alguma idéia ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Como está o codigo completo? Posta ai, posso te ajudar nessa!

 

Testei aqui e rodou:

 

 

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<script language="javascript">
window.onload = function(){
somatudo = (10 * 1) + (20 * 1);
somatudogeral = somatudo.toFixed(2);
document.getElementById("totalgeral").value = somatudogeral;
}
</script>
<title>HTML5</title>
</head>
<body>
<input name="totalgeral" type="text" id="totalgeral" style="height:20px; width:80px;" />
</form>
</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Desse jeito que você postou funciona... mas se tentar pegar dos campos só funciona no internet explorer mesmo...

olha o codigo completo ai:

 

<script>

function soma0(){

sa = document.getElementById("qtde0").value;

sb = document.getElementById("vlr0").value;

rsa = sa*sb;

vss = rsa.toFixed(2);

document.getElementById("total0").value = vss;

}

</script><script>

function soma1(){

sa = document.getElementById("qtde1").value;

sb = document.getElementById("vlr1").value;

rsa = sa*sb;

vss = rsa.toFixed(2);

document.getElementById("total1").value = vss;

}

</script>

<script>

function somatotal()

{

somatudo = (form.total0.value*1) + (form.total1.value*1);

somatudogeral = somatudo.toFixed(2);

document.getElementById("totalgeral").value = somatudogeral;

}

</script>

 

<form name="form" id="form">

<table border="0" align="center" cellpadding="1" cellspacing="1">

<tr>

<td><strong> Produto</strong></td>

 

<td><strong>Quantidade</strong></td>

<td><strong>Valor Unitário</strong></td>

<td><strong>Valor Total</strong></td>

</tr>

<tr>

<td>Produto 1</td>

 

<td><input name="qtde0" type="text" class="formcampo" id="qtde0" onkeyup="soma0();somatotal();" value="5" /></td>

<td><input name="vlr0" type="hidden" id="vlr0" value="55.60" />55,60</td>

<td><input name="total0" type="text" class="campoform1" id="total0" style="height:20px; width:80px;" value="278.00" /></td>

</tr>

<tr>

<td>Produto 2</td>

<td><input name="qtde1" type="text" class="formcampo" id="qtde1" onkeyup="soma1();somatotal();" value="1" /></td>

<td><input name="vlr1" type="hidden" id="vlr1" value="850.00" />850,00</td>

 

<td align="center" bgcolor="#F0F0F0"><input name="total1" type="text" class="campoform1" id="total1" style="height:20px; width:80px;" value="850.00" /></td>

</tr>

<tr>

<td> </td>

<td> </td>

<td> </td>

<td><input name="totalgeral" type="text" id="totalgeral" style="height:20px; width:80px;" value="1128.00" class="formcampo" /></td>

</tr>

</table>

</form>

 

 

Compartilhar este post


Link para o post
Compartilhar em outros sites

Essa função está errada:

function somatotal(){
somatudo = (form.total0.value*1) + (form.total1.value*1);
somatudogeral = somatudo.toFixed(2);
document.getElementById("totalgeral").value = somatudogeral;
}

 

Você esqueceu de declara o formulário no DOM.

formulario = document.form;

 

Veja o correto

function somatotal(){
formulario = document.form;
somatudo = (formulario.total0.value*1) + (formulario.total1.value*1);
somatudogeral = somatudo.toFixed(2);
document.getElementById("totalgeral").value = somatudogeral;
}

 

Teste ai agora amigo!

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.