Actualización de la suma total. Javascript .. im seguro de algo pequeño ..

  • Mozzi
  • Student
  • Student
  • No Avatar
  • Registrado: Ago 19, 2004
  • Mensajes: 70
  • Status: Offline

Nota Mayo 20th, 2010, 3:27 am

Hola .. ¿alguien puede ayudar por favor .. no puedo obtener mi formulario para actualizar la SUMA TOTAL...he probado muchas cosas .. pero soy un novato .. y asegurarse de que sus debe ser algo simple...

Código: [ Select ]

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/JavaScript">
function calculate(col, row){
var numericExpression = /^[0-9]+$/; //verify numeric - needs to allow for decimals though...hmmm...
  if(document.getElementById(col+"-"+row).value.match(numericExpression)){
var input=0; //initalize variable to get entered value
var rowTot=0; //initalize variable to capture row totals
var colTot=0; //initalize variable to capture column totals
//setup up array for paycodes/column headers
codes=new Array('1000','1001','1002','1005','1007','1003','1020','1035','1038','1045','1066','1067','Other');
//loop through all of the text fields for a given column and total them
    for (i=0;i<2;i++){
    input=(document.getElementById(col+'-'+i).value)*1
    colTot=colTot+input;
    document.getElementById(col).value = colTot;
    }
//loop through all of the text fields for a given row and total them
    for (j=0;j<13;j++){
    input=(document.getElementById(((codes[j])+'-'+row)).value)*1
    rowTot=rowTot+input;
    document.getElementById('row'+row).value = rowTot;
    }
  }
  //if the entry isn't numeric then return no value
  else {document.getElementById(col+"-"+row).value="";}
}

function updatesum() {
document.form.sum.value = (document.form.sum1.value -0) + (document.form.sum2.value -0);
}

</script>

</head>

<body>

<p>Enter numbers here:<br>
<form name="form" >
 <p>
  <input name="1000-0" id="1000-0" type="text" onblur="calculate('1000','0')">
  <input name="1001-0" id="1001-0" type="text" onblur="calculate('1001','0')">
  <br>
  <input name="1000-1" id="1000-1" type="text" onblur="calculate('1000','1')">
  <input name="1001-1" id="1001-1" type="text" onblur="calculate('1001','1')">
  <br>
  <input name="sum1" id="1000" type="text" onblur="updatesum()" >
  <input name="sum2" id="1001" type="text" onblur="updatesum()">
<br>

  Their sum is:
  <input name="sum" readonly>

</form>
</body>
</html>
  1. <head>
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <title>Untitled Document</title>
  4. <script type="text/JavaScript">
  5. function calculate(col, row){
  6. var numericExpression = /^[0-9]+$/; //verify numeric - needs to allow for decimals though...hmmm...
  7.   if(document.getElementById(col+"-"+row).value.match(numericExpression)){
  8. var input=0; //initalize variable to get entered value
  9. var rowTot=0; //initalize variable to capture row totals
  10. var colTot=0; //initalize variable to capture column totals
  11. //setup up array for paycodes/column headers
  12. codes=new Array('1000','1001','1002','1005','1007','1003','1020','1035','1038','1045','1066','1067','Other');
  13. //loop through all of the text fields for a given column and total them
  14.     for (i=0;i<2;i++){
  15.     input=(document.getElementById(col+'-'+i).value)*1
  16.     colTot=colTot+input;
  17.     document.getElementById(col).value = colTot;
  18.     }
  19. //loop through all of the text fields for a given row and total them
  20.     for (j=0;j<13;j++){
  21.     input=(document.getElementById(((codes[j])+'-'+row)).value)*1
  22.     rowTot=rowTot+input;
  23.     document.getElementById('row'+row).value = rowTot;
  24.     }
  25.   }
  26.   //if the entry isn't numeric then return no value
  27.   else {document.getElementById(col+"-"+row).value="";}
  28. }
  29. function updatesum() {
  30. document.form.sum.value = (document.form.sum1.value -0) + (document.form.sum2.value -0);
  31. }
  32. </script>
  33. </head>
  34. <body>
  35. <p>Enter numbers here:<br>
  36. <form name="form" >
  37.  <p>
  38.   <input name="1000-0" id="1000-0" type="text" onblur="calculate('1000','0')">
  39.   <input name="1001-0" id="1001-0" type="text" onblur="calculate('1001','0')">
  40.   <br>
  41.   <input name="1000-1" id="1000-1" type="text" onblur="calculate('1000','1')">
  42.   <input name="1001-1" id="1001-1" type="text" onblur="calculate('1001','1')">
  43.   <br>
  44.   <input name="sum1" id="1000" type="text" onblur="updatesum()" >
  45.   <input name="sum2" id="1001" type="text" onblur="updatesum()">
  46. <br>
  47.   Their sum is:
  48.   <input name="sum" readonly>
  49. </form>
  50. </body>
  51. </html>
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Mayo 20th, 2010, 3:27 am

  • Cae
  • Expert
  • Expert
  • Avatar de Usuario
  • Registrado: Feb 25, 2004
  • Mensajes: 734
  • Status: Offline

Nota Mayo 24th, 2010, 8:05 pm

El código funciona bien en mi equipo, de hecho, funciona exactamente como debería.

Su problema es que sus dos complementos inicial (1.000 / 0, 1001 / 0 y 1000 1 /, 1001 / 1) llamar a calcular cuando se desenfoque. Esto actualiza los campos suma (sum1, sum2), sin embargo este disparador de actualización no pasa por sumar los campos, es posible que se hayan actualizado, pero aún no han sido seleccionados por el usuario y por lo tanto, no se puede perder el foco.

La solución es simple, sin embargo, sólo llame updatesum () al final de la rutina a calcular.

Publicar Información

  • Total de mensajes en este tema: 2 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 105 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC