Ver Mensaje Individual
  #2 (permalink)  
Antiguo 03/10/2013, 07:07
bathorz
 
Fecha de Ingreso: agosto-2013
Mensajes: 150
Antigüedad: 10 años, 8 meses
Puntos: 29
Respuesta: suma de decimales en tiempo real en javascript

Pruebalo así, luego intenta descomentado return o //continue
Código HTML:
Ver original
  1. <script type="text/javascript">
  2.    function sumar(c) { //alert(c);
  3.       var campo = document.getElementById('form1');
  4.       var subtotal = 0;
  5.  
  6.       if (!/^d*$/.test(c)) // <-- problema, busca solución
  7.         //return;
  8.      
  9.      for (var i = 0; i < campo.length - 1; i++) {
  10.         if (!/^d+$/.test(campo[i].value)) // <-- problema, busca solución
  11.           // continue;
  12.         subtotal += parseFloat(campo[i].value);
  13.      }
  14.       document.form1.res.value = subtotal;
  15.   }
  16.  
  17. <form id="form1" name="form1" action="" method="post">
  18. 1:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
  19. 2:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
  20. 3:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
  21. 4:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
  22. 5:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
  23. 6:<input type="text" name="sum[]" value="1.1" onKeyUp="sumar(this.value);" /><br />
  24.    Resultado:
  25.    <input type="text" id="res" name="res" value="0" />
  26. </form>