Tema: Sumar Arrays
Ver Mensaje Individual
  #7 (permalink)  
Antiguo 10/10/2011, 00:00
Avatar de _Andrea_
_Andrea_
 
Fecha de Ingreso: octubre-2011
Ubicación: Medellin
Mensajes: 15
Antigüedad: 12 años, 8 meses
Puntos: 0
Respuesta: Sumar Arrays

Muchas gracias tu ayuda me ha servido demasiado, te pido disculpas por molestarte tanto, gracias, pero tengo una duda;
Cuando hago lo siguiente: agrego un tr o una tabla más para darles nombres a los campos no me realiza nada de la operación, anexo el código con lo que realice nuevo:

Agregando tr

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Documento sin t&iacute;tulo</title>
  5.  
  6. </head>
  7.  
  8.  
  9. <form name="autoSumForm">
  10. <table align="center">
  11. <tr>
  12.                 <th class="Estilo1" scope="col">Primera</th>
  13.                 <th class="Estilo1" scope="col"><div align="center">Segunda</div></th>
  14.                 <th class="Estilo1" scope="col"><div align="center">Tercera</div></th>
  15.                 </tr>
  16.  
  17. <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>
  18. <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>
  19. <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>
  20. <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>
  21. </form>
  22.  
  23. function Sumas(nombre_form){
  24. var f = document.forms[nombre_form],T = f.getElementsByTagName('tr'),i=0,l=T.length,Sum,all,
  25.  S = function(Prim,Seg,Ter){
  26.  // Proceso de suma
  27.  Sum = function(){Ter.value = (parseInt(Prim.value,10) || 0) + (parseInt(Seg.value,10) || 0)}
  28.  Seg.onkeyup = Sum
  29.  Ter.onfocus = Sum
  30.  Ter.onblur = Sum
  31.  return !0
  32. }, byName = function(T,name){
  33.  // Buscar atributo name
  34.  all = T.getElementsByTagName('*'),j=0
  35.  while(all[j].getAttribute('name') != name && j<all.length) j++
  36. return all[j]
  37. }
  38. // Sumar al teclear, salir de la caja, seleccionar caja...
  39. while(i<l && S(byName(T[i],'cantidad[]'), byName(T[i],'caja[]'), byName(T[i],'resultado[]'))) i++
  40. }
  41.  
  42. Sumas('autoSumForm')
  43. </script>
  44.  
  45.  
  46. </body>
  47. </html>

Agregando Tabla, aunque esta me interesa más.

Código Javascript:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Documento sin t&iacute;tulo</title>
  6.  
  7. </head>
  8.  
  9. <body>
  10. <form name="autoSumForm">
  11.   <table width="200" border="0">
  12.   <tr>
  13.     <th scope="col">Primera</th>
  14.     <th scope="col">Segunda</th>
  15.     <th scope="col">Tercera</th>
  16.   </tr>
  17.   <tr>
  18.     <td><input type='text' name='cantidad[]2' value='1'* /></td>
  19.     <td><input type='text' name='caja[]2'/></td>
  20.     <td><input type='text' name='resultado[]2' /></td>
  21.   </tr>
  22.   <tr>
  23.     <td><input type='text' name='cantidad[]3' value='56' /></td>
  24.     <td><input type='text' name='caja[]3'/></td>
  25.     <td><input type='text' name='resultado[]3' /></td>
  26.   </tr>
  27.   <tr>
  28.     <td><input type='text' name='cantidad[]4' value='7'* /></td>
  29.     <td><input type='text' name='caja[]4'/></td>
  30.     <td><input type='text' name='resultado[]4' /></td>
  31.   </tr>
  32.   <tr>
  33.     <td><input type='text' name='cantidad[]' value='10' /></td>
  34.     <td><input type='text' name='caja[]'/></td>
  35.     <td><input type='text' name='resultado[]' /></td>
  36.   </tr>
  37. </table>
  38. </form>
  39.  
  40. <script>
  41. function Sumas(nombre_form){
  42. var f = document.forms[nombre_form],T = f.getElementsByTagName('tr'),i=0,l=T.length,Sum,all,
  43.  S = function(Prim,Seg,Ter){
  44.  // Proceso de suma
  45.  Sum = function(){Ter.value = (parseInt(Prim.value,10) || 0) + (parseInt(Seg.value,10) || 0)}
  46.  Seg.onkeyup = Sum
  47.  Ter.onfocus = Sum
  48.  Ter.onblur = Sum
  49.  return !0
  50.  }, byName = function(T,name){
  51.  // Buscar atributo name
  52.  all = T.getElementsByTagName('*'),j=0
  53.  while(all[j].getAttribute('name') != name && j<all.length) j++
  54.  return all[j]
  55.  }
  56.  // Sumar al teclear, salir de la caja, seleccionar caja...
  57.  while(i<l && S(byName(T[i],'cantidad[]'), byName(T[i],'caja[]'), byName(T[i],'resultado[]'))) i++
  58. }
  59.  
  60. Sumas('autoSumForm')
  61. </script>
  62.  
  63. </body>

Te pido mis más sinceras disculpas por esto.

Gracias

Última edición por _Andrea_; 10/10/2011 a las 00:25