Ir para conteúdo

POWERED BY:

Arquivado

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

Boris

[Resolvido] Soma de 2 campos

Recommended Posts

Ola galera sou novo no Ajax mas estou precisando de uma ajuda de vcs...

 

tenho tres campos Box

 

valor1

valor2

total

 

Eu procurei mas não encontrei nda semelhante em AJAX que no momento que eu coloco o valor no campo, valor1 e valor2 ele aparece já a soma no campo total.

 

Algue poderia me dar uma ajuda....

 

Obrigado

Compartilhar este post


Link para o post
Compartilhar em outros sites

mas pk ajax? os valores sao preenchidos por você ou vao ser carregados a uma pagina?

 

se nao forem carregados a uma pagina pode fazer assim

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function soma(){
v1=document.getElementById("valor1").value;
v2=document.getElementById("valor2").value;
total=document.getElementById("total");
if((v1!="") && (v2!=""))
{
total.value=eval(v1)+eval(v2);
}

}
</script>
</head>

<body>
<label>valor1:<input type="text" value="" id="valor1"  onblur="soma()"/></label>
<label>valor2:<input type="text" value="" id="valor2"  onblur="soma()"/></label>
<label>valor3:<input type="text" value="" id="total"  readonly="readonly"/></label>

</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Eu fiz assim mas está dando problema

 

<script language="javascript">

function Soma(){

document.formulario.frm_master_subtotal.value = (parseFloat(document.formulario.frm_master_debito.value) + parseFloat(document.formulario.frm_master_credito.value));

}

Codigo inteiro

 

<html>

<head>

<title>SOMA</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">

function Soma(){

document.formulario.frm_master_subtotal.value = (parseFloat(document.formulario.frm_master_debito.value) + parseFloat(document.formulario.frm_master_credito.value));

}

</script>

<script language=JavaScript>

function currencyFormat(fld, milSep, decSep, e) {

var sep = 0;

var key = '';

var i = j = 0;

var len = len2 = 0;

var strCheck = '0123456789';

var aux = aux2 = '';

var whichCode = (window.Event) ? e.which : e.keyCode;

if (whichCode == 13) return true; // Enter

key = String.fromCharCode(whichCode); // Get key value from key code

if (strCheck.indexOf(key) == -1) return false; // Not a valid key

len = fld.value.length;

for(i = 0; i < len; i++)

if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;

aux = '';

for(; i < len; i++)

if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);

aux += key;

len = aux.length;

if (len == 0) fld.value = '';

if (len == 1) fld.value = '0'+ decSep + '0' + aux;

if (len == 2) fld.value = '0'+ decSep + aux;

if (len > 2) {

aux2 = '';

for (j = 0, i = len - 3; i >= 0; i--) {

if (j == 3) {

aux2 += milSep;

j = 0;

}

aux2 += aux.charAt(i);

j++;

}

fld.value = '';

len2 = aux2.length;

for (i = len2 - 1; i >= 0; i--)

fld.value += aux2.charAt(i);

fld.value += decSep + aux.substr(len - 2, len);

}

return false;

}</SCRIPT>

</head>

 

<body bgcolor="#FFFFFF" text="#000000">

<form name="formulario" method="post" action="">

<input name="frm_master_debito" type="text" class="FormBoxCartao" id="frm_master_debito2" size="8" maxlength="10" onKeyPress="return(currencyFormat(this,',','.',event))" onBlur="Soma();">

<br>

<input name="frm_master_credito" type="text" class="FormBoxCartao" id="frm_master_credito" size="8" maxlength="10" onKeyPress="return(currencyFormat(this,',','.',event))" onBlur="Soma();">

<br>

<br>

<input name="frm_master_subtotal" type="text" class="FormBoxCartao" id="frm_master_subtotal" onKeyPress="return(currencyFormat(this,',','.',event))" value="0,00" size="8" maxlength="10">

<br>

</form>

</body>

</html>

Só que estou tendo problema pra somar os centavos:

 

Por Exemplo se eu colocar

Valor 1 = 1,000.00

Valor 2 = 1,000.00

Total = 2

 

Sendo que deveria aparecer 2,000.00

 

ou

 

Valor 1 = 1,500.00

Valor 2 = 1,500.00

Total = 2

 

Ele continua aparecendo 2

 

Alguem pode me ajudar...

 

Obrigado

Compartilhar este post


Link para o post
Compartilhar em outros sites

tire os pontos e as virgulas que funciona...depois você formata o resultado..

Compartilhar este post


Link para o post
Compartilhar em outros sites

ok, realmente funfo...

 

Mas como eu faço pra aparecer a virgula e o ponto...

 

O que acontece o usuario não vai conseguir indentificar corretamente o valor que ele estará digitando e por esse motivo eu precisaria que aparecesse o mesmo.

 

alguem poderia me ajudar... obrigado...

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.