Tema: Sumar Arrays
Ver Mensaje Individual
  #6 (permalink)  
Antiguo 09/10/2011, 21:17
Avatar de America|UNK
America|UNK
 
Fecha de Ingreso: noviembre-2006
Ubicación: Piura - Perú
Mensajes: 582
Antigüedad: 18 años, 3 meses
Puntos: 56
Respuesta: Sumar Arrays

Asi es la estructura de tu tabla de sumas?
Código javascript:
Ver original
  1. <form name="autoSumForm">
  2. <table>
  3. <tr><td><input type='text' name='cantidad[]' value='1'  />+</td><td><input type='text' name='caja[]'/>=</td><td><input type='text' name='resultado[]' /></td></tr>
  4. <tr><td><input type='text' name='cantidad[]' value='56' />+</td><td><input type='text' name='caja[]'/>=</td><td><input type='text' name='resultado[]' /></td></tr>
  5. <tr><td><input type='text' name='cantidad[]' value='7'  />+</td><td><input type='text' name='caja[]'/>=</td><td><input type='text' name='resultado[]' /></td></tr>
  6. <tr><td><input type='text' name='cantidad[]' value='10' />+</td><td><input type='text' name='caja[]'/>=</td><td><input type='text' name='resultado[]' /></td></tr>
  7. </table>
  8. </form>
  9.  
  10. <script>
  11. function Sumas(nombre_form){
  12.     var f = document.forms[nombre_form],T = f.getElementsByTagName('tr'),i=0,l=T.length,Sum,all,
  13.     S = function(Prim,Seg,Ter){
  14.         // Proceso de suma
  15.         Sum = function(){Ter.value = (parseInt(Prim.value,10) || 0) + (parseInt(Seg.value,10) || 0)}
  16.         Seg.onkeyup = Sum
  17.         Ter.onfocus = Sum
  18.         Ter.onblur = Sum
  19.         return !0
  20.     }, byName = function(T,name){
  21.         // Buscar atributo name
  22.         all = T.getElementsByTagName('*'),j=0
  23.         while(all[j].getAttribute('name') != name && j<all.length) j++
  24.         return all[j]
  25.     }
  26.         // Sumar al teclear, salir de la caja, seleccionar caja...
  27.     while(i<l && S(byName(T[i],'cantidad[]'), byName(T[i],'caja[]'), byName(T[i],'resultado[]'))) i++
  28. }
  29.  
  30. Sumas('autoSumForm')
  31. </script>

Si tienes alguna duda, avisame.
__________________
/* El que atiende, entiende..., el que entiende, aprende!.
Desarrollo Web Freelance, Contactar */