mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-17 14:30:28 +00:00
116 lines
No EOL
4.9 KiB
HTML
116 lines
No EOL
4.9 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1"><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
|
|
<div data-role="page">
|
|
<div data-role="header">
|
|
<h1>jQuery.param()</h1>
|
|
|
|
</div>
|
|
<div data-role="content" data-theme="c" id="jQuery.param1">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">jQuery.param( obj )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#String">String</a></span>
|
|
</h2>
|
|
<div class=" entry-details">
|
|
<p class="desc"><strong>Description: </strong>Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request. </p>
|
|
<ul class="signatures">
|
|
<li class="signature" id="jQuery.param-obj">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.2/">1.2</a></span>jQuery.param( obj )</h4>
|
|
<p class="arguement"><strong>obj</strong>An array or object to serialize.</p>
|
|
</li>
|
|
<li class="signature" id="jQuery.param-obj-traditional">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>jQuery.param( obj, traditional )</h4>
|
|
<p class="arguement"><strong>obj</strong>An array or object to serialize.</p>
|
|
<p class="arguement"><strong>traditional</strong>A Boolean indicating whether to perform a traditional "shallow" serialization.</p>
|
|
</li>
|
|
</ul>
|
|
<div class="longdesc">
|
|
<p>This function is used internally to convert form element values into a serialized string representation (See <a href="/serialize/">.serialize()</a> for more information).</p>
|
|
<p>As of jQuery 1.3, the return value of a function is used instead of the function as a String.</p>
|
|
<p>As of jQuery 1.4, the <code>$.param()</code> method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting <code>jQuery.ajaxSettings.traditional = true;</code>.</p>
|
|
<p>Note: Because some frameworks have limited ability to parse serialized arrays, we should exercise caution when passing an <code>obj</code> argument that contains objects or arrays nested within another array.</p>
|
|
<p>In jQuery 1.4 HTML5 input elements are serialized, as well.</p>
|
|
<p>We can display a query string representation of an object and a URI-decoded version of the same as follows:</p>
|
|
<pre>var myObject = {
|
|
a: {
|
|
one: 1,
|
|
two: 2,
|
|
three: 3
|
|
},
|
|
b: [1,2,3]
|
|
};
|
|
var recursiveEncoded = $.param(myObject);
|
|
var recursiveDecoded = decodeURIComponent($.param(myObject));
|
|
|
|
alert(recursiveEncoded);
|
|
alert(recursiveDecoded);
|
|
</pre>
|
|
<p>The values of <code>recursiveEncoded</code> and <code>recursiveDecoded</code> are alerted as follows:</p>
|
|
<p><span class="output">a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3</span><br><span class="output">a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3</span></p>
|
|
<p>To emulate the behavior of <code>$.param()</code> prior to jQuery 1.4, we can set the <code>traditional</code> argument to <code>true</code>:</p>
|
|
<pre>var myObject = {
|
|
a: {
|
|
one: 1,
|
|
two: 2,
|
|
three: 3
|
|
},
|
|
b: [1,2,3]
|
|
};
|
|
var shallowEncoded = $.param(myObject, true);
|
|
var shallowDecoded = decodeURIComponent(shallowEncoded);
|
|
|
|
alert(shallowEncoded);
|
|
alert(shallowDecoded);
|
|
</pre>
|
|
<p>The values of <code>shallowEncoded</code> and <code>shallowDecoded</code> are alerted as follows:</p>
|
|
<p><span class="output">a=%5Bobject+Object%5D&b=1&b=2&b=3</span><br><span class="output">a=[object+Object]&b=1&b=2&b=3</span></p>
|
|
</div>
|
|
<h3>Examples:</h3>
|
|
<div id="entry-examples" class="entry-examples">
|
|
<div id="example-0">
|
|
<h4>Example: <span class="desc">Serialize a key/value object.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>div { color:red; }</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<div id="results"></div>
|
|
<script>
|
|
|
|
var params = { width:1680, height:1050 };
|
|
var str = jQuery.param(params);
|
|
$("#results").text(str);
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-1">
|
|
<h4>Example: <span class="desc">Serialize a few complex objects</span>
|
|
</h4>
|
|
<pre><code class="example">
|
|
// <=1.3.2:
|
|
$.param({ a: [2,3,4] }) // "a=2&a=3&a=4"
|
|
// >=1.4:
|
|
$.param({ a: [2,3,4] }) // "a[]=2&a[]=3&a[]=4"
|
|
|
|
// <=1.3.2:
|
|
$.param({ a: { b:1,c:2 }, d: [3,4,{ e:5 }] }) // "a=[object+Object]&d=3&d=4&d=[object+Object]"
|
|
// >=1.4:
|
|
$.param({ a: { b:1,c:2 }, d: [3,4,{ e:5 }] }) // "a[b]=1&a[c]=2&d[]=3&d[]=4&d[2][e]=5"
|
|
|
|
</code></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body></html> |