Ver Mensaje Individual
  #10 (permalink)  
Antiguo 13/06/2014, 12:32
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Sumar al pulsar un botón

Algo asi ?

Código Javascript:
Ver original
  1. <form action="" method="">
  2.  
  3.     <input type="text" value="4" id="a" size="2"/>
  4.  
  5.     <a href="javascript:inc_count('a');"> <img src="http://blogs.unlp.edu.ar/didacticaytic/files/2013/07/sumar-1.jpg" width="20" height="20" /></a>
  6.     <p/>
  7.     <input type="text" value="7" id="b" size="2"/>
  8. <a href="javascript:inc_count('b');"> <img src="http://blogs.unlp.edu.ar/didacticaytic/files/2013/07/sumar-1.jpg" width="20" height="20" /></a>
  9.    
  10. </form>
  11.  
  12. <script>
  13. function inc_count(id)
  14. {
  15.     res = document.getElementById(id);
  16.     res.value = parseInt(res.value)+1;
  17. }
  18. </script>

Otra opcion... un poco mas eficiente porque evita que debas leer el valor del DOM (arbol de nodos de elementos "HTML"), seria solo actualizar (update) pero leer de las variables globales los valores:

Código Javascript:
Ver original
  1. <script>
  2. // valores iniciales
  3. a=0;
  4. b=0;
  5. </script>
  6.  
  7.  
  8. <form action="" method="">
  9.  
  10.     <input type="text" id="a" size="2"/>
  11.  
  12.     <a href="javascript:a++; update(a);"> <img src="http://blogs.unlp.edu.ar/didacticaytic/files/2013/07/sumar-1.jpg" width="20" height="20" /></a>
  13.     <p/>
  14.     <input type="text"  id="b" size="2"/>
  15.    
  16.     <a href="javascript:b++; update(b);"> <img src="http://blogs.unlp.edu.ar/didacticaytic/files/2013/07/sumar-1.jpg" width="20" height="20" /></a>
  17.    
  18. </form>
  19.  
  20. <script>
  21.     input_a = document.getElementById('a');
  22.     input_b = document.getElementById('b');
  23.  
  24.     function update()
  25.     {
  26.         input_a.value=a;
  27.         input_b.value=b;
  28.     }
  29.    
  30.     update();
  31. </script>

<< es algo mas "rapida"
__________________
Salu2!

Última edición por Italico76; 13/06/2014 a las 12:39