Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/10/2013, 13:04
and_zxxy
 
Fecha de Ingreso: julio-2013
Ubicación: Ibarra
Mensajes: 13
Antigüedad: 10 años, 9 meses
Puntos: 0
Respuesta: suma de decimales en tiempo real en javascript

Código HTML:
<script type="text/javascript">
   function sumar(c) { //alert(c);
      var campo = document.getElementById('form1');
      var subtotal = 0;

      if (!/^d*$/.test(c)) // <-- problema, busca solución
         //return;
      
      for (var i = 0; i < campo.length - 1; i++) { 
         if (!/^d+$/.test(campo[i].value)) // <-- problema, busca solución
           // continue;
         subtotal += parseFloat(campo[i].value);
      }
       document.form1.res.value = subtotal;
   }
</script>

<form id="form1" name="form1" action="" method="post">
1:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
2:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
3:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
4:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
5:<input type="text" name="sum[]" value="0" onKeyUp="sumar(this.value);" /><br />
6:<input type="text" name="sum[]" value="1.1" onKeyUp="sumar(this.value);" /><br />
   Resultado:
   <input type="text" id="res" name="res" value="0" />
   <input type="submit" value="enviar" />
   <input type="submit" name="enviar2" id="enviar2" value="enviar2" />
</form> 

disculpa

pero si en el ejemplo ahi le aumento 2 boton enviar ya no me suma y me sale error (NaN)

se le puede corregir eso para que si me sume.....??

Ayuda por favor....