Ver Mensaje Individual
  #48 (permalink)  
Antiguo 26/08/2008, 14:49
Avatar de buzu
buzu
 
Fecha de Ingreso: octubre-2006
Ubicación: San Francisco, CA
Mensajes: 2.168
Antigüedad: 18 años, 4 meses
Puntos: 122
Respuesta: Recorrer tabla

Aquí un código similar al tuyo, genera los elementos de forma dinámica y les asigna un name distinto y único. Luego los recorre y da un alert por fila el cual contiene los datos de dichos input.

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Untitled</title>
</head>
<body>
<form name="formulario">
	<table>
		<tr>
			<th>1</th>
			<th>2</th>
			<th>3</th>
			<th>4</th>
		</tr>
		<script type="text/javascript">
			var i = 0;
			while(i<10){
				document.write('<tr>');
				document.write('<td>1 '+i+'<\/td>');
				document.write('<td>2 '+i+'<\/td>');
				document.write('<td><input type="text" name="cant'+i+'" /><\/td>');
				document.write('<td><input type="text" name="und'+i+'" /><\/td>');
				document.write('<\/tr>');
				i++;
			}
		</script>
		<tr>
			<td><input type="button" onclick='datosTexto()' /></td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	function datosTexto(){
		for(i=0;i<10;i++){
			var cant = "cant";
			var und = "und";
			//alert(document.formulario[cant + i.toString()])
			cant = document.formulario[cant + i.toString()];
			und = document.formulario[und + i.toString()];
	
			if ((und.value != " ") && (cant.value != " ")){
				alert(und.value+" "+cant.value);
			}

		}

	}

</script>
</body>
</html> 
__________________
twitter: @imbuzu