• 0
Sign in to follow this  
cdfree

excluindo casa decimal

Question

Não sei por que não exibi a primeira casa decimal, e se tiver uma forma de resumir este código eu aceito sugestão.

<!-- &lt;script src=moeda.js language="javascript"></script> -->

<form action=script.php method=post>

<table border=0><tr><td>

01/20XX<input type="text" name=vl1 onkeypress="mudanome();soma();" id="nome" onKeyPress="return(MascaraMoeda(this,'.',',',event))"><br>

02/20XX<input type="text" name=vl2 id="vl2" onkeypress="soma();"/><br>

03/20XX<input type="text" name=vl3 id="vl3" onkeypress="soma();"/><br>

04/20XX<input type="text" name=vl4 id="vl4" onkeypress="soma();"/></td><td>

05/20XX<input type="text" name=vl5 id="vl5" onkeypress="soma();"/><br>

06/20XX<input type="text" name=vl6 id="vl6" onkeypress="soma();"/><br>

07/20XX<input type="text" name=vl7 id="vl7" onkeypress="soma();"/><br>

08/20XX<input type="text" name=vl8 id="vl8" onkeypress="soma();"/></td><td>

09/20XX<input type="text" name=vl9 id="vl9" onkeypress="soma();"/><br>

10/20XX<input type="text" name=vl10 id="vl10" onkeypress="soma();"/><br>

11/20XX<input type="text" name=vl11 id="vl11" onkeypress="soma();"/><br>

12/20XX<input type="text" name=vl12 id="vl12" onkeypress="soma();"/></td></tr><tr>

<td colspan=2>VALOR TOTAL DO CONTRATO</td><td style='text-align:right'><input type="text" id="total" /></tr></tr>

</table>

<input type=submit value=enviar><input type=reset value=limpar>

</form>

&lt;script>

function mudanome()

{

for(i=1; i<=12; i++){

document.getElementById("vl"+i).value = document.getElementById("nome").value;

}

}

function soma(){

var c1 = parseFloat(document.getElementById("vl1").value);

var c2 = parseFloat(document.getElementById("vl2").value);

var c3 = parseFloat(document.getElementById("vl3").value);

var c4 = parseFloat(document.getElementById("vl4").value);

var c5 = parseFloat(document.getElementById("vl5").value);

var c6 = parseFloat(document.getElementById("vl6").value);

var c7 = parseFloat(document.getElementById("vl7").value);

var c8 = parseFloat(document.getElementById("vl8").value);

var c9 = parseFloat(document.getElementById("vl9").value);

var c10 = parseFloat(document.getElementById("vl10").value);

var c11 = parseFloat(document.getElementById("vl11").value);

var c12 = parseFloat(document.getElementById("vl12").value);

if (isNaN(c1)) c1 = 0;

if (isNaN(c2)) c2 = 0;

if (isNaN(c3)) c3 = 0;

if (isNaN(c4)) c4 = 0;

if (isNaN(c5)) c5 = 0;

if (isNaN(c6)) c6 = 0;

if (isNaN(c7)) c7 = 0;

if (isNaN(c8)) c8 = 0;

if (isNaN(c9)) c9 = 0;

if (isNaN(c10)) c10 = 0;

if (isNaN(c11)) c11 = 0;

if (isNaN(c12)) c12 = 0;

var soma = parseFloat(c1+c2+c3+c4+c5+c6+c7+c8+c9+c10+c11+c12);

document.getElementById("total").value = parseFloat(soma.toFixed(2));

}

</script>

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Seus erros:

  • Voce colocou 2 vezes no 1º item o onKeyPress
  • E convenhamos, que seu site vai ficar meio lento se voce atualizar toda hora que o cara digitar um numero.
Eu reformulei seu código e troquei:
  • onKeyPress por Onblur
  • Parte significativa do seu script
  • E agora, não importa se o cara escrever "," ou "." seu script vai conseguir somar normal.
Ta ai o que eu fiz:

<!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>Documento sem título</title>
&lt;script>
function mudanome()
{
for(i=1; i<=12; i++){
document.getElementById("vl"+i).value = document.getElementById("nome").value;

}
}
function soma(){
c = new Array;
for(i=2;i<=12;i++){
c[i] = 0;
}
for(i=2;i<=12;i++){
c[i] = parseFloat(document.getElementById("vl"+i).value.replace(",","."));
if(isNaN(c[i]) || c[i]==""){
c[i] =0;
}
}
c[1]= parseFloat(document.getElementById("nome").value.replace(",","."));
if(c[1]=="" || isNaN(c[1])){
c[1]=0;
}
var soma = c[1]+c[2]+c[3]+c[4]+c[5]+c[6]+c[7]+c[8]+c[9]+c[10]+c[11];
document.getElementById("total").value = soma.toFixed(2);
}


</script>
</head>

<body>
<form action="script.php" method="post">
<table border="0"><tr><td>
01/20XX<input type="text" name="vl1" onblur="soma();" id="nome" onKeyPress="return(MascaraMoeda(this,'.',',',event));"/><br>
02/20XX<input type="text" name="vl2" id="vl2" onblur="soma();"/><br>
03/20XX<input type="text" name="vl3" id="vl3" onblur="soma();"/><br>
04/20XX<input type="text" name="vl4" id="vl4" onblur="soma();"/></td><td>
05/20XX<input type="text" name="vl5" id="vl5" onblur="soma();"/><br>
06/20XX<input type="text" name="vl6" id="vl6" onblur="soma();"/><br>
07/20XX<input type="text" name="vl7" id="vl7" onblur="soma();"/><br>
08/20XX<input type="text" name="vl8" id="vl8" onblur="soma();"/></td><td>
09/20XX<input type="text" name="vl9" id="vl9" onblur="soma();"/><br>
10/20XX<input type="text" name="vl10" id="vl10" onblur="soma();"/><br>
11/20XX<input type="text" name="vl11" id="vl11" onblur="soma();"/><br>
12/20XX<input type="text" name="vl12" id="vl12" onblur="soma();"/></td></tr><tr>
<td colspan=2>VALOR TOTAL DO CONTRATO</td><td style='text-align:right'><input type="text" id="total" /></tr></tr>
</table>



<input type="submit" value="enviar"><input type="reset" value="limpar">

</form>

</body>
</html>[/codebox]

Só copiar e colar! Like a :ninja:

**Nota : só testei no Dreamweaver mesmo... <_<

Edited by Arash

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this