Autor Tema: [AYUDA] SUMAR VALORES, JS  (Leído 726 veces)

Desconectado maska

  • PHPer@ Fijo
  • ***
  • Mensajes: 126
  • Karma: 2
  • Nuev@ PHPer@
    • Ver Perfil
[AYUDA] SUMAR VALORES, JS
« en: 28 de Julio de 2012, 12:40:59 pm »
Por un lado, deseo sumar varios SELECT en vertical. Y por otro, sumar en vertical varios INPUT.

Los INPUT lo he conseguido. Sería así:

Código: [Seleccionar]
<html>
<head>
<script type="text/javascript">
function sumar(c){
var subtotal = 0;
campo = c.form;
  if(!/^\d*$/.test(c.value)) return;

      for (var i = 0; i < campo.length-1; i++) {
          if (!/^\d+$/.test(campo[i].value)) continue;
              subtotal += parseInt(campo[i].value);
      }
document.getElementById('res').value = subtotal;
}
</script>
</head>
<body>
<form name="a" action="" method="post">
  <input type="text" name="sum[]" value="0" onkeyup="sumar(this);" />
  <input type="text" id="res" value="0" readonly="readonly" />
</form><!-- fin de form sumar -->
</body>
</html>

¿Y los SELECT? ¿Cómo se sumarían en vertical mediante JS?

Comunidad PHPeros

[AYUDA] SUMAR VALORES, JS
« en: 28 de Julio de 2012, 12:40:59 pm »