Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/09/2010, 06:54
Avatar de Adler
Adler
Colaborador
 
Fecha de Ingreso: diciembre-2006
Mensajes: 4.671
Antigüedad: 17 años, 4 meses
Puntos: 126
Respuesta: Comprobar un número indefinido de campos

Hola
Cita:
Iniciado por Alun Ver Mensaje
¿Alguna idea de cómo hacer lo que pretendo, pero que funcione?
Aún no me he cruzado con nadie que responda con el objetivo de confundir. Unas veces puedes estar mas acertado y otras menos, pero la pretensión es siempre, y repito, siempre, ofrecer la ayuda mas satisfactoria posible

Prueba con esto
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. <script type="text/javascript">
  6. function crearCampos(cantidad){
  7. var div = document.getElementById("campos_dinamicos");
  8. while(div.firstChild)div.removeChild(div.firstChild); // remover elementos;
  9.     for(var i = 1, cantidad = Number(cantidad); i <= cantidad; i++){
  10.     var salto = document.createElement("P");
  11.     var input = document.createElement("input");
  12.     var text = document.createTextNode("Campo Dinamico " + i + ": ");
  13.     input.setAttribute("name", "campo[]");
  14.     input.setAttribute("id", "campo" + i);
  15.     input.setAttribute("size", "12");
  16.     input.className = "input";
  17.     salto.appendChild(text);
  18.     salto.appendChild(input);
  19.     div.appendChild(salto);
  20.     }
  21. }
  22. </script>
  23.  
  24. </head>
  25. <body>
  26. <form>
  27. ¿Cuantos Campos? <input type="text" name="cantidad" id="cantidad" value="" onkeyup="crearCampos(this.value);" />
  28. <input type="button" id="boton" value="Crear/Eliminar Campos" onclick="crearCampos(this.form.cantidad.value);" />
  29. <div id="campos_dinamicos"></div>
  30. </form>
  31. </body>
  32. </html>
Suerte
__________________
Los formularios se envían/validan con un botón Submit
<input type="submit" value="Enviar" style="background-color:#0B5795; font:bold 10px verdana; color:#FFF;" />