Skip to content

Commit 3813a0b

Browse files
committed
.serialize(): Clarify what elements it operates on.
Fixes #10
1 parent 15c9c63 commit 3813a0b

1 file changed

Lines changed: 11 additions & 33 deletions

File tree

entries/serialize.xml

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,25 @@
66
</signature>
77
<desc>Encode a set of form elements as a string for submission.</desc>
88
<longdesc>
9-
<p>The <code>.serialize()</code> method creates a text string in standard URL-encoded notation. It operates on a jQuery object representing a set of form elements. The form elements can be of several types:</p>
10-
<pre><code>&lt;form&gt;
11-
&lt;div&gt;&lt;input type="text" name="a" value="1" id="a" /&gt;&lt;/div&gt;
12-
&lt;div&gt;&lt;input type="text" name="b" value="2" id="b" /&gt;&lt;/div&gt;
13-
&lt;div&gt;&lt;input type="hidden" name="c" value="3" id="c" /&gt;&lt;/div&gt;
14-
&lt;div&gt;
15-
&lt;textarea name="d" rows="8" cols="40"&gt;4&lt;/textarea&gt;
16-
&lt;/div&gt;
17-
&lt;div&gt;&lt;select name="e"&gt;
18-
&lt;option value="5" selected="selected"&gt;5&lt;/option&gt;
19-
&lt;option value="6"&gt;6&lt;/option&gt;
20-
&lt;option value="7"&gt;7&lt;/option&gt;
21-
&lt;/select&gt;&lt;/div&gt;
22-
&lt;div&gt;
23-
&lt;input type="checkbox" name="f" value="8" id="f" /&gt;
24-
&lt;/div&gt;
25-
&lt;div&gt;
26-
&lt;input type="submit" name="g" value="Submit" id="g" /&gt;
27-
&lt;/div&gt;
28-
&lt;/form&gt;</code></pre>
29-
<p>The <code>.serialize()</code> method can act on a jQuery object that has selected individual form elements, such as <code>&lt;input&gt;</code>, <code>&lt;textarea&gt;</code>, and <code>&lt;select&gt;</code>. However, it is typically easier to select the <code>&lt;form&gt;</code> tag itself for serialization:</p>
30-
<pre><code>$('form').submit(function() {
31-
alert($(this).serialize());
32-
return false;
9+
<p>The <code>.serialize()</code> method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as <code>&lt;input&gt;</code>, <code>&lt;textarea&gt;</code>, and <code>&lt;select&gt;</code>: <code>$( "input, textarea, select" ).serialize();</code></p>
10+
<p>It is typically easier, however, to select the <code>&lt;form&gt;</code> itself for serialization:</p>
11+
<pre><code>$( "form" ).on( "submit", function( event ) {
12+
event.preventDefault();
13+
console.log( $(this).serialize() );
3314
});</code></pre>
34-
<p>This produces a standard-looking query string:</p>
35-
<pre><code>a=1&amp;b=2&amp;c=3&amp;d=4&amp;e=5</code></pre>
15+
<p>In this case, jQuery serializes the successful controls within the form.</p>
3616
<p><strong>Warning:</strong> selecting both the form and its children will cause duplicates in the serialized string.</p>
3717
<p>Note: Only <a href="http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2">"successful controls"</a> are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a <code>name</code> attribute. Values from checkboxes and radio buttons (<code>input</code>s of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.</p>
3818
</longdesc>
3919
<example>
40-
<desc>Serialize a form to a query string, that could be sent to a server in an Ajax request.</desc>
20+
<desc>Serialize a form to a query string that could be sent to a server in an Ajax request.</desc>
4121
<code><![CDATA[
4222
function showValues() {
4323
var str = $("form").serialize();
44-
$("#results").text(str);
24+
$("#results").text( str );
4525
}
46-
$(":checkbox, :radio").click(showValues);
47-
$("select").change(showValues);
26+
$("input[type='checkbox'], input[type='radio']").on( "click", showValues );
27+
$("select").on( "change", showValues );
4828
showValues();
4929
]]></code>
5030
<css><![CDATA[
@@ -71,11 +51,9 @@
7151
</select>
7252
<br/>
7353
<input type="checkbox" name="check" value="check1" id="ch1"/>
74-
7554
<label for="ch1">check1</label>
7655
7756
<input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/>
78-
7957
<label for="ch2">check2</label>
8058
<br />
8159
<input type="radio" name="radio" value="radio1" checked="checked" id="r1"/>
@@ -90,4 +68,4 @@
9068
<category slug="forms"/>
9169
<category slug="ajax/helper-functions"/>
9270
<category slug="version/1.0"/>
93-
</entry>
71+
</entry>

0 commit comments

Comments
 (0)