Foros del Web » Programando para Internet » Jquery »

Limpiar campo tras envio con jquery

Estas en el tema de Limpiar campo tras envio con jquery en el foro de Jquery en Foros del Web. Hola, he hecho un formulario para enviar por jquery y recibir la vuelta, pero el problema es que los campos no se limpian tras el ...
  #1 (permalink)  
Antiguo 13/08/2009, 12:50
 
Fecha de Ingreso: mayo-2005
Mensajes: 51
Antigüedad: 18 años, 11 meses
Puntos: 0
Pregunta Limpiar campo tras envio con jquery

Hola, he hecho un formulario para enviar por jquery y recibir la vuelta, pero el problema es que los campos no se limpian tras el envio. He de decir que mis conocimiento de ajax son bastante limitados ( la verdad, es solo lo necesito para un par de cosas sueltas), asi que quiza mis intentos hayam sido "pateticos xD".

He probado con --$("#name").val("");--, durante la respuesta q recibe del archivo php, pero no funciona, y la verdad es q no si esta bien puesto.
Código HTML:
<!doctype html>
<html lang="en">
<head>
	<title>jQuery UI Dialog - Modal form</title>
	<link type="text/css" href="../../themes/base/ui.all.css" rel="stylesheet" />
	<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
	<script type="text/javascript" src="../../ui/ui.core.js"></script>
	<script type="text/javascript" src="../../ui/ui.draggable.js"></script>
	<script type="text/javascript" src="../../ui/ui.resizable.js"></script>
	<script type="text/javascript" src="../../ui/ui.dialog.js"></script>
	<script type="text/javascript" src="../../ui/effects.core.js"></script>
	<script type="text/javascript" src="../../ui/effects.highlight.js"></script>
	<script type="text/javascript" src="../../external/bgiframe/jquery.bgiframe.js"></script>
	<link type="text/css" href="../demos.css" rel="stylesheet" />
	<style type="text/css">
		body { font-size: 62.5%; }
		label, input { display:block; }
		input.text { margin-bottom:12px; width:95%; padding: .4em; }
		fieldset { padding:0; border:0; margin-top:25px; }
		h1 { font-size: 1.2em; margin: .6em 0; }
		div#users-contain {  width: 350px; margin: 20px 0; }
		div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
		div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
		.ui-button { outline: 0; margin:0; padding: .4em 1em .5em; text-decoration:none;  !important; cursor:pointer; position: relative; text-align: center; }
		.ui-dialog .ui-state-highlight, .ui-dialog .ui-state-error { padding: .3em;  }
		
		
	</style>
	<script type="text/javascript">
	$(function() {
		
		var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				return false;
			} else {
				return true;
			}

		}

function cargad(){
    $("#info").load('http://192.168.1.129:8080/c/index.php/noticias/pver/FTJgN:9j9n53X:g8dxgevQLO:4t44Imt2th6umGYACFoju:6sr3d3', {limit:2} );
	};
 $(document).ready(function(){
cargad();
setInterval( cargad, 5000 );
  });
  
		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			modal: true,
			buttons: {
				'Create an account': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(name,"username",3,16);
					bValid = bValid && checkLength(email,"email",3,80);
					bValid = bValid && checkLength(password,"password",5,16);


					bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: [url]http://projects.scottsplayground.com/email_address_validation/[/url]
					bValid = bValid && checkRegexp(email,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
					
					if (bValid) {
               				$.post('http://192.168.1.129:8080/c/index.php/link/ajax/',
		                        {pid:name.val(), em:email.val(), dr:password.val()},
										  function(datos) {
		   $('#users tbody').append(datos);
		   $('name').val("");
		  });           
						$(this).dialog('close');
					}
				}
			},

		});

	});
	

	$(document).ready(function() {

	$().ajaxStart(function() {
		$('#loading').show();
		$('#result').hide();
	}).ajaxStop(function() {
		$('#loading').hide();
		$('#result').fadeIn('slow');
	});

	$('#form, #fat, #fo3').submit(function() {
		$.ajax({
			type: 'POST',
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(data) {
				$('#result').html(data);

			}
		})
		
		return false;
	});
	
		$('#fo2').submit(function() {
		$.ajax({
			type: 'POST',
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: 										  function(datos) {
		   $('#users tbody').append(datos);
			}
		})
		
		return false;
	});
});


	</script>
</head>
<body>

<div class="demo">

<div id="dialog" title="Create new user">
	<p id="validateTips">All form fields are required.</p>

</div>

<div id="info"></div>

<div id="users-contain" class="ui-widget">

		<h1>Existing Users:</h1>


	<table id="users" class="ui-widget ui-widget-content">
		<thead>
			<tr class="ui-widget-header ">
				<th>Name</th>
				<th>Email</th>
				<th>Password</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>John Doe</td>
				<td>[email protected]</td>
				<td>johndoe1</td>
			</tr>
		</tbody>
	</table>
</div>
<form method="post" action="http://192.168.1.129:8080/c/index.php/link/ajax/" id="fo2" name="fo2" onSubmit="clear()" >
	<fieldset>
		<label for="name">Name</label>
		<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
		<label for="email">Email</label>
		<input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
		<label for="password">Password</label>
		<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
		<input type="text" id="f2f" value="">
		<input type="submit" id="login" value="Login" name="login"/>
	</fieldset>
	</form>
<button id="create-user" class="ui-button ui-state-default ui-corner-all">Create new user</button>
<form method="post">
<input type="button" value="Reload Window">
</form>
</div><!-- End demo -->

<div class="demo-description">

<p>Use a modal dialog to require that the user enter data during a multi-step process.  Embed form markup in the content area, set the <code>modal</code> option to true, and specify primary and secondary user actions with the <code>buttons</code> option.</p>

</div><!-- End demo-description -->

</body>
</html> 
El codigo es uno de ejemplo que trae, JqueryUI, y que he adaptado a mis necesidades.

Si alguien puede ayudarme se lo agradeceria mucho ^^
  #2 (permalink)  
Antiguo 13/08/2009, 13:39
Avatar de goyo_  
Fecha de Ingreso: agosto-2009
Mensajes: 91
Antigüedad: 14 años, 8 meses
Puntos: 1
Respuesta: Limpiar campo tras envio con jquery

Te falto la '#' en la siguiente linea:

Código HTML:
$('name').val("");
Cambialo a:
Código HTML:
$('#name').val("");
  #3 (permalink)  
Antiguo 13/08/2009, 13:44
 
Fecha de Ingreso: mayo-2005
Mensajes: 51
Antigüedad: 18 años, 11 meses
Puntos: 0
Respuesta: Limpiar campo tras envio con jquery

Hola, no es eso, he probado a ponerlo bien añadiendo el #, pero nada.

salu2
  #4 (permalink)  
Antiguo 19/08/2009, 01:14
 
Fecha de Ingreso: agosto-2009
Mensajes: 1
Antigüedad: 14 años, 8 meses
Puntos: 0
Respuesta: Limpiar campo tras envio con jquery

Hola, creo que lo más sensato sería que crearas una funcion js que limpie los datos, o simplemente hacer un reset del form.

SALU2
  #5 (permalink)  
Antiguo 09/10/2009, 13:16
 
Fecha de Ingreso: octubre-2009
Mensajes: 104
Antigüedad: 14 años, 6 meses
Puntos: 0
De acuerdo Respuesta: Limpiar campo tras envio con jquery

Hola me parece que lo que quieres, si estas trabajando con JQuery es poner algo como esto:

Código PHP:
$('form')[0].reset(); 
....

Saludos.
  #6 (permalink)  
Antiguo 10/10/2009, 11:13
 
Fecha de Ingreso: mayo-2005
Mensajes: 51
Antigüedad: 18 años, 11 meses
Puntos: 0
Respuesta: Limpiar campo tras envio con jquery

Muchas gracias, no habia conseguido arreglarlo y use otra forma mas rudimentarea, pero de esta forme me ahorras muchos problemas.

salu2
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 06:43.