Ver Mensaje Individual
  #4 (permalink)  
Antiguo 28/12/2016, 14:40
mpozo
 
Fecha de Ingreso: noviembre-2015
Mensajes: 231
Antigüedad: 8 años, 5 meses
Puntos: 86
Respuesta: mostrar valores de suma de 2 array

Como decimos por aquí "Más vale una imagen que cien palabras". Así que me guiaré por la imagen
Código Javascript:
Ver original
  1. <!DOCTYPE html>
  2. <html dir="ltr" lang="es-es">
  3.     <head>
  4.         <title></title>
  5.         <meta charset="utf-8">
  6.         <meta name="viewport" content="user-scalable=yes, width=device-width, initial-scale=1">
  7.         <style>
  8.            
  9.         </style>
  10.         <script>
  11.             document.addEventListener('DOMContentLoaded', function() {
  12.                 new foo();
  13.             }, false);
  14.  
  15.  
  16.             function foo() {
  17.                 this.dato1 = document.querySelectorAll('input[name="dato1[]"]');
  18.                 this.dato2 = document.querySelectorAll('input[name="dato2[]"]');
  19.                 this.suma = document.querySelectorAll('div[name="test[]"]');
  20.  
  21.                 var _this = this;
  22.                 [].forEach.call(_this.dato1, function(campo, indice) {
  23.  
  24.                     campo.addEventListener('keyup', function() {
  25.  
  26.                         _this.calcular(indice);
  27.  
  28.                     }, false);
  29.  
  30.                     _this.dato2[indice].addEventListener('keyup', function() {
  31.  
  32.                         _this.calcular(indice);
  33.  
  34.                     }, false);
  35.                 })
  36.             }
  37.  
  38.             foo.prototype.calcular = function(i) {
  39.                 var dato1 = parseFloat(this.dato1[i].value) || 0,
  40.                 dato2 = parseFloat(this.dato2[i].value) || 0;
  41.  
  42.                 this.suma[i].textContent = dato1 + dato2;
  43.             }
  44.         </script>
  45.     </head>
  46.     <body>
  47.  
  48.         <table>
  49.             <tr>
  50.                 <td><input type="text" name="dato1[]" value="0" size="12"></td>
  51.                 <td><input type="text" name="dato2[]" value="0" size="12"></td>
  52.                 <td><div name="test[]"> </div></td>
  53.             </tr>
  54.             <tr>
  55.                 <td><input type="text" name="dato1[]" value="0" size="12"></td>
  56.                 <td><input type="text" name="dato2[]" value="0" size="12"></td>
  57.                 <td><div name="test[]"> </div></td>
  58.             </tr>
  59.         </table>
  60.        
  61.     </body>
  62. </html>