Ver Mensaje Individual
  #3 (permalink)  
Antiguo 10/09/2011, 20:48
tazzwt
 
Fecha de Ingreso: marzo-2010
Mensajes: 432
Antigüedad: 14 años, 2 meses
Puntos: 11
Respuesta: Un getElementById dinamico realizar suma

No se como usar el getElementsByName, bueno este es el codigo que tengo:

Código Javascript:
Ver original
  1. <?php
  2.  
  3. include("conexion.php");
  4. $link=Conectarse();
  5.  
  6. echo "
  7. <script language='JavaScript' type='text/javascript'>  
  8. function suma(){
  9.    
  10. var capacidad=4;
  11. var i=0;
  12. var textfield = new Array();
  13. while (i < capacidad) {
  14.    textfield[i] = document.getElementById('textfield['+i+']').value;
  15.    
  16.     alert(textfield[i].value);
  17.    i++;
  18. }
  19.    
  20. }      
  21.  
  22. </script>  
  23. ";
  24.  
  25.  
  26. $result=mysql_query("select * from articulos",$link);
  27.  
  28.  
  29. $x=0;
  30. while($row = mysql_fetch_array($result)) {
  31. $nombre = $row["nombre"];
  32. $precio = $row["precio"];
  33.  
  34. echo "$nombre <input type=\"text\" value=\"textfield$x\" id=\"textfield$x\"/><br>";
  35.  
  36. $x++;
  37. }
  38.  
  39. echo "<br>";
  40. echo "El total es: <input type='text' id='resultado'><br>";
  41.  
  42. echo "<br>";
  43.  
  44. echo " <input type='button' name='Submit' value='Sumar' onclick='suma()'> ";
  45.  
  46. mysql_close($link);
  47.  
  48. ?>
__________________
Libro de HTML 5 + codigo fuente bajar aqui.