<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codigo Manso &#187; form</title>
	<atom:link href="http://www.codigomanso.com/es/tag/form/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codigomanso.com</link>
	<description>Programación, informática y tecnología</description>
	<lastBuildDate>Sun, 21 Aug 2011 10:54:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Extensión jQuery para serializar formularios</title>
		<link>http://www.codigomanso.com/es/2008/12/extension-jquery-para-serializar-formularios/</link>
		<comments>http://www.codigomanso.com/es/2008/12/extension-jquery-para-serializar-formularios/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 09:42:44 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[send]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[unchecked]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/?p=257</guid>
		<description><![CDATA[jQuery ya dispone de un método llamado serialize, que básicamente transforma un formulario en una cadena típica de un POST o un GET, bastante útil para enviar un formulario con una petición al más puro estilo AJAX tal cual lo hace el navegador.
El problema es que a mi no me gusta lo que hace el [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">jQuery ya dispone de un método llamado <strong>serialize</strong>, que básicamente transforma un formulario en una cadena típica de un POST o un GET, bastante útil para enviar un formulario con una petición al más puro estilo AJAX tal cual lo hace el navegador.</p>
<p style="text-align: justify;">El problema es que a mi no me gusta lo que hace el navegador con los &#8220;checkboxes&#8221;.</p>
<p style="text-align: justify;">Como cualquier otro input, los checkboxes tienen un valor asociado (por defecto &#8220;on&#8221;, aunque no estoy seguro de si esto varía según el navegador). Lo que uno esperaría de value, es que sea &#8220;on&#8221; si está checked y &#8220;off&#8221; si está unchecked. Pero no señores, no se confundan, siempre es &#8220;on&#8221;, igual que si le dices que value=&#8221;pepito&#8221;, value siempre será &#8220;pepito&#8221; independientemente de si el checkbox está checkeado o no.</p>
<p style="text-align: justify;">Entonces (se preguntará el lector avispado que aún no haya tenido el placer de programar en HTML), ¿como sabe el servidor si el checkbox está checked o unchecked? Elemental mi querido Watson. El navegador sólo envia esa información si está checked y no la envia si está unchecked. ¿Que putada, no? Porque hay veces que parece que esto rompe algunos flujos típicos con otros datos.</p>
<p style="text-align: justify;">No es que este comportamiento de los navegadores sea muy problematico, pero a veces a uno le interesa que funcione de otra manera.</p>
<p style="text-align: justify;">Sea como fuere, aquí pongo una extensión para jQuery, que básicamente lo que hace es lo mismo que &#8220;serialize&#8221; pero pone siempre los checkboxes, de tal forma que pone &#8220;<strong>nombre=1</strong>&#8221; cuando el checkbox está activado, y &#8220;<strong>nombre=0</strong>&#8221; cuando no está activado.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">jserialize</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> serialized <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">':checkbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> tofind    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> toreplace <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">checked</span><span style="color: #009900;">&#41;</span>   <span style="color: #009900;">&#123;</span> serialized <span style="color: #339933;">=</span> serialized.<span style="color: #660066;">replace</span> <span style="color: #009900;">&#40;</span>tofind<span style="color: #339933;">,</span> toreplace<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span>                <span style="color: #009900;">&#123;</span> serialized <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;&amp;amp;&quot;</span> <span style="color: #339933;">+</span> toreplace<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> serialized<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Enlaces de interés:</strong></p>
<ul>
<li><a href="http://www.codigomanso.com/2008/12/resetear-un-formulario-con-jquery/" target="_blank">Resetear un formulario con jQuery</a></li>
<li><a href="http://groups.google.com/group/jquery-en/browse_thread/thread/a73638a11636299d/ed6a56666cc8347a?#ed6a56666cc8347a" target="_blank">Pregunta de Badtant sobre porque serialize no envia los unchecked checkboxes</a></li>
<li><a href="http://www.jquery.com" target="_blank">Página oficial de jQuery</a></li>
<li><a href="http://docs.jquery.com/Ajax/serialize" target="_blank">Método jQuery.serialize</a></li>
<li><a href="http://docs.jquery.com/Ajax/serializeArray" target="_blank">Método jQuery.serializeArray</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/es/2008/12/extension-jquery-para-serializar-formularios/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Resetear un formulario con jquery</title>
		<link>http://www.codigomanso.com/es/2008/12/resetear-un-formulario-con-jquery/</link>
		<comments>http://www.codigomanso.com/es/2008/12/resetear-un-formulario-con-jquery/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 09:30:44 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[formulario]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[reset]]></category>
		<category><![CDATA[resetear formulario]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/?p=240</guid>
		<description><![CDATA[Una cosa que se suele hacer bastante en javascript es resetear un formulario, es decir, devolver el formulario a su valor inicial.
Con jquery esto es bastante fácil:

$&#40;'#Formulario'&#41;.each &#40;function&#40;&#41;&#123;
  this.reset&#40;&#41;;
&#125;&#41;;

El tema está en que el código de arriba itera sobre cada elemento que está dentro de form, y llama al método &#8220;reset&#8221; del DOM de [...]]]></description>
			<content:encoded><![CDATA[<p>Una cosa que se suele hacer bastante en javascript es resetear un formulario, es decir, devolver el formulario a su valor inicial.</p>
<p>Con jquery esto es bastante fácil:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#Formulario'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>El tema está en que el código de arriba itera sobre cada elemento que está dentro de form, y llama al método &#8220;reset&#8221; del DOM de javascript (ojo que no se hace un $(this).reset()). La función &#8220;reset&#8221; en jquery no existe.</p>
<p>Sería más sencillo si se pudiera hacer <strong>$(&#8220;#Formulario&#8221;).reset()</strong> y listo, ¿verdad?. Bueno, por suerte crear esa función únicamente requiere que perdamos 1 minuto programando:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">reset</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ahora, una vez definido el código de arriba, ya podemos hacer un reset llamando a &#8220;reset&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#Formulario&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Tal vez usar &#8220;<strong>reset</strong>&#8221; es demasiado genérico para ser aplicado a cualquier objeto, aunque creo que tiene bastante sentido usar un nombre tan sencillo, pero quien quiera que lo renombre a &#8220;resetForm&#8221;.</p>
<p>Enlaces de interés:</p>
<ul>
<li><a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery" target="_blank">Getting started with jquery:</a> interesante echarle un ojo <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li><a href="http://www.learningjquery.com/2007/08/clearing-form-data" target="_blank">Clearing form data:</a> como borrar los datos en un formulario</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/es/2008/12/resetear-un-formulario-con-jquery/feed/</wfw:commentRss>
		<slash:comments>65</slash:comments>
		</item>
	</channel>
</rss>

