Foros del Web » Programando para Internet » Javascript »

agregar input a un form

Estas en el tema de agregar input a un form en el foro de Javascript en Foros del Web. Hola, lo que necesito hacer es agregar un un input a un from cuando se hace click en un link. por ej: Cita: <form action="session.php" ...
  #1 (permalink)  
Antiguo 20/06/2008, 18:03
 
Fecha de Ingreso: junio-2008
Mensajes: 110
Antigüedad: 15 años, 10 meses
Puntos: 0
agregar input a un form

Hola, lo que necesito hacer es agregar un un input a un from cuando se hace click en un link.

por ej:

Cita:
<form action="session.php" method="get" enctype="text/plain">

<table>
<tr>
<td><input type="text" name="nombre"> <a href="#" >crear nuevo</a></td>

</tr>
</table>

</form>
La verdad que no tengo ni idea de javascript, me pueden ayudar?
  #2 (permalink)  
Antiguo 20/06/2008, 19:15
 
Fecha de Ingreso: septiembre-2004
Mensajes: 252
Antigüedad: 19 años, 7 meses
Puntos: 0
Respuesta: agregar input a un form

Podria ser algo asi
Código:
<!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="en" lang="en">

<head>
	<title>sin título</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<meta name="generator" content="Geany 0.14" />
	<script type="text/javascript">
	/*<![CDATA[*/
	var c=0;
	function newInput()
	{
		var inpt = document.createElement('input');
		inpt.type="text";
		inpt.name="input_"+c;
		inpt.id="input_"+c;
		c+=1;
		document.f1.appendChild(inpt);
		document.f1.innerHTML+="<br/>";
	}
	/*]]>*/
	</script>
</head>

<body>
<a href="javascript:newInput()" >crear nuevo</a>
<form action="session.php" method="get"  name="f1">


</form>	
</body>
</html>
  #3 (permalink)  
Antiguo 20/06/2008, 19:51
 
Fecha de Ingreso: junio-2008
Mensajes: 110
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: agregar input a un form

gracias, pero estoy buscando algo mas siemple que me permita entenderlo a mi.

Código:
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function agrega_fila(id){
    var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
    var row = document.createElement("TR")
    var td1 = document.createElement("TD")
    var inpu = ducument.createElement("in")
    td1.appendChlid(inpu);
	row.appendChild(td1);
    tbody.appendChild(row);
  }
</script>
</HEAD>

<BODY>

<form action="rrhh.php" method="get" enctype="text/plain">
<table name="tabla" id="cc">
  <tbody>
	<tr>
		<td><input type="text" name="idioma" id="idiom"><a href="javascript:agregar_fila('cc')">[+] Agregar</a></td>
	</tr>
  </tbody>
</table>

</form>

</body>
Algo asi?, pero no me anda.
  #4 (permalink)  
Antiguo 21/06/2008, 02:55
Avatar de JavierB
Colaborador
 
Fecha de Ingreso: febrero-2002
Ubicación: Madrid
Mensajes: 25.052
Antigüedad: 22 años, 2 meses
Puntos: 772
Respuesta: agregar input a un form

Hola anion0x

Echa un vistazo a esta página: crear elementos

Saludos,
  #5 (permalink)  
Antiguo 21/06/2008, 05:13
 
Fecha de Ingreso: junio-2008
Mensajes: 110
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: agregar input a un form

muchas gracias, lo estuve leyendo pero no logro terminar de hacer lo que necesito. Aca esta el codigo que pude hacer, pero no me funciona del todo.

Cita:
<script type="text/javascript">
num=0;
function crear(obj) {
num++;
fi = document.getElementById('fiel'); // 1

ele = document.createElement('input'); // 5
ele.type = 'text'; // 6
ele.name = 'fil'+num; // 6
fi.appendChild(ele); // 7
fi.insertBefore(fi,obj);
}
</script>
Lo que quiero es cada vez que se haga un click en [+] Agregar idioma me agrege dos input y dos label.

Cita:
<tr id="fiel">

<th><label for="idiomas">Idiomas</label></th>
<td><input type="text" name="idiomas" id="idiomas" value="<?=v('idiomas')?>" /></td>
<th><label for="domIdioma">% de dominio</label></th>
<td><input class="short" type="text" name="domIdioma" id="domIdioma" value="<?=v('domIdioma')?>" /> <a href="javascript:crear('fiel')">[+] Agregar idioma</a></td>

</tr>
O sea, que me los vaya agregado debajo de ese.
  #6 (permalink)  
Antiguo 21/06/2008, 18:25
 
Fecha de Ingreso: junio-2008
Mensajes: 110
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: agregar input a un form

Con los ejemplos que encontre pude llegar a a hacer esto, pero no lo puedo terminar.

Cita:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test jQuery</title>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.highlightFade.js"></script>

<script type="text/javascript">
function addFormField() {
var id = document.getElementById("idioma").value;
$("#divTxt").append("<p id='row" + id + "'><label for='txt" + id + "'>Idioma &nbsp;<input type='text' size='20' name='txt[]' id='txt" + id + "'>&nbsp;&nbsp<a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;'>Quitar</a><p>");

id = (id - 1) + 2;
document.getElementById("id").value = id;
}

function removeFormField(id) {
$(id).remove();
}
</script>

<body>


<form action="#" method="get" id="form1">

<div id="divTxt">
<tr id="fiel">
<th><label for="idiomas">Idioma</label></th>
<td><input type="text" name="idiomas" id="idioma" value="" /></td>
<th><label for="domIdioma">% de dominio</label></th>
<td><input class="short" type="text" name="domIdioma" id="domidioma" value="" />
</tr>
</div>
<p><a href="#" onClick="addFormField(); return false;">[+] Agregar</a></p>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>

</body>
</html>
Lo que necesitaria es que se agregue esto cuando hago click.

Cita:
<tr id="fiel">
<th><label for="idiomas">Idioma</label></th>
<td><input type="text" name="idiomas" id="idioma" value="" /></td>
<th><label for="domIdioma">% de dominio</label></th>
<td><input class="short" type="text" name="domIdioma" id="domidioma" value="" />
</tr>
Alguna idea?

PD: faltan algunos tags de table, por ej, pero creo que para ejemplo no hacen falta.
  #7 (permalink)  
Antiguo 21/06/2008, 18:37
 
Fecha de Ingreso: junio-2008
Mensajes: 110
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: agregar input a un form

pude hacer lo que necesito, ahora me falta poder manejarlo desde PHP, como podria hacer?

Cita:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test jQuery</title>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.highlightFade.js"></script>

<script type="text/javascript">
function addFormField() {
//$("#divTxt").append("<p id='row'><label for='txt'>Idioma &nbsp;<input type='text' size='20' name='txt[]' id='txt'>&nbsp;&nbsp<a href='#' onClick='removeFormField(\"#row\"); return false;'>Quitar</a><p>");
$("#divTxt").append("<p id='row'><label for='txt'>Idioma &nbsp;<input type='text' size='20' name='txt[]' id='txt'>&nbsp;&nbsp<label for='txt1'>% de dominio &nbsp;<input type='text' size='20' name='txt[]' id='txt1'>&nbsp;&nbsp<a href='#' onClick='removeFormField(\"#row\"); return false;'>Quitar</a><p>");



}

function removeFormField(id) {
$(id).remove();
}
</script>

<body>


<form action="#" method="get" id="form1">

<div id="divTxt">
<tr id="fiel">
<th><label for="idiomas">Idioma</label></th>
<td><input type="text" name="idiomas" id="idioma" value="" /></td>
<th><label for="domIdioma">% de dominio</label></th>
<td><input class="short" type="text" name="domIdioma" id="domidioma" value="" />
</tr>
</div>
<p><a href="#" onClick="addFormField(); return false;">[+] Agregar</a></p>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>
</body>
</html>
  #8 (permalink)  
Antiguo 22/06/2008, 03:06
Avatar de JavierB
Colaborador
 
Fecha de Ingreso: febrero-2002
Ubicación: Madrid
Mensajes: 25.052
Antigüedad: 22 años, 2 meses
Puntos: 772
Respuesta: agregar input a un form

Hola de nuevo.

La parte de PHP, pregunta en el foro de PHP, allí podrán ayudarte mejor.

Saludos,
  #9 (permalink)  
Antiguo 22/06/2008, 06:31
 
Fecha de Ingreso: junio-2008
Mensajes: 110
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: agregar input a un form

lo que no se es que nombre le esta poniendo a cada elemento que crea

Código:
<input type='text' size='20' name='txt[]' id='txt'>&nbsp;&nbsp<label for='txt1'>
que hace esto?

Código:
'txt[]'
  #10 (permalink)  
Antiguo 22/06/2008, 08:40
 
Fecha de Ingreso: junio-2008
Mensajes: 110
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: agregar input a un form

Estuve haciendo algo como esto:

Código:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test jQuery</title>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.highlightFade.js"></script>

<script type="text/javascript">
var contador = 0;

function addFormField() {
	$("#divTxt").append("<input type=\"text\" name=\"idioma + contador \">");
	contador++;
}
</script>

<body>

<p><a href="#" onClick="addFormField(); return false;">Add</a></p>

<form action="#" method="get" id="form1">

<div id="divTxt"></div>
<?php  
	echo $_GET['idioma'];

?>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>

</body>
</html>
La idea es tener un contador, y que cada vez que se lanza el evento onclick me agregue un input, y que el nombre sea
idioma0
idioma1
idioma2
para luego poder manejarlos desde php, pero no se como terminarlo.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:24.