<div class="blogpost-body" id="cnblogs_post_body">
<div class="cnblogs_code">
<img alt="" class="code_img_closed" id="code_img_closed_3aee2ec0-7cfe-4d23-8708-1dc394da17f1" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8f900a89c6347c561fdf2122f13be562.gif">
<img alt="" class="code_img_opened" id="code_img_opened_3aee2ec0-7cfe-4d23-8708-1dc394da17f1" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-961ddebeb323a10fe0623af514929fc1.gif">
<div class="cnblogs_code_hide" id="cnblogs_code_open_3aee2ec0-7cfe-4d23-8708-1dc394da17f1">
<pre class="blockcode"><span style="color:#008080;"> 1</span> <span style="color:#008000;">//</span><span style="color:#008000;">Serialize an array of form elements or a set of</span>
<span style="color:#008080;"> 2</span> <span style="color:#008000;">//</span><span style="color:#008000;">key/values into a query string</span>
<span style="color:#008080;"> 3</span> <span style="color:#008000;">//</span><span style="color:#008000;"> 将数组形式的表单元素或者哈希表序列化成字符串</span>
<span style="color:#008080;"> 4</span> jQuery.param = <span style="color:#0000ff;">function</span><span style="color:#000000;">(a, traditional) {
</span><span style="color:#008080;"> 5</span> <span style="color:#0000ff;">var</span> prefix, s =<span style="color:#000000;"> [],
</span><span style="color:#008080;"> 6</span> add = <span style="color:#0000ff;">function</span><span style="color:#000000;">(key, value) {
</span><span style="color:#008080;"> 7</span> <span style="color:#008000;">//</span><span style="color:#008000;"> If value is a function, invoke it and return its value</span>
<span style="color:#008080;"> 8</span> <span style="color:#008000;">//</span><span style="color:#008000;"> 如果value是函数就执行并返回执行结果</span>
<span style="color:#008080;"> 9</span> value = jQuery.isFunction(value) ? value() : (value == <span style="color:#0000ff;">null</span> ? ''<span style="color:#000000;"> : value);
</span><span style="color:#008080;"> 10</span> s[s.length] = encodeURIComponent(key) + '=' +
<span style="color:#008080;"> 11</span> <span style="color:#000000;"> encodeURIComponent(value);
</span><span style="color:#008080;"> 12</span> <span style="color:#000000;"> };
</span><span style="color:#008080;"> 13</span>
<span style="color:#008080;"> 14</span> <span style="color:#008000;">//</span><span style="color:#008000;"> Set traditional to true for jQuery <= 1.3.2 behavior</span>
<span style="color:#008080;"> 15</span> <span style="color:#0000ff;">if</span> (traditional ===<span style="color:#000000;"> undefined) {
</span><span style="color:#008080;"> 16</span> traditional = jQuery.ajaxSettings &&<span style="color:#000000;"> jQuery.ajaxSettings.traditional;
</span><span style="color:#008080;"> 17</span> <span style="color:#000000;"> }
</span><span style="color:#008080;"> 18</span>
<span style="color:#008080;"> 19</span> <span style="color:#008000;">//</span><span style="color:#008000;"> If an array was passed in, assume that it is an array of form elements.</span>
<span style="color:#008080;"> 20</span> <span style="color:#008000;">//</span><span style="color:#008000;"> 如果传进来的是数组,假设是表单元素</span>
<span style="color:#008080;"> 21</span> <span style="color:#0000ff;">if</span> (jQuery.isArray(a) || (a.jquery && !<span style="color:#000000;">jQuery.isPlainObject(a))) {
</span><span style="color:#008080;"> 22</span> <span style="color:#008000;">//</span><span style="color:#008000;"> 序列化表单元素</span>
<span style="color:#008080;"> 23</span> jQuery.each(a, <span style="color:#0000ff;">function</span><span style="color:#000000;">() {
</span><span style="color:#008080;"> 24</span> add(<span style="color:#0000ff;">this</span>.name, <span style="color:#0000ff;">this</span><span style="color:#000000;">.value);
</span><span style="color:#008080;"> 25</span> <span style="color:#000000;"> });
</span><span style="color:#008080;"> 26</span>
<span style="color:#008080;"> 27</span> } <span style="color:#0000ff;">else</span><span style="color:#000000;"> {
</span><span style="color:#008080;"> 28</span> <span style="color:#008000;">//</span><span style="color:#008000;"> If traditional, encode the "old" way (the way 1.3.2 or older</span>
<span style="color:#008080;"> 29</span> <span style="color:#008000;">//</span><span style="color:#008000;"> did it), otherwise encode params recursively.</span>
<span style="color:#008080;"> 30</span> <span style="color:#0000ff;">for</span> (prefix <span style="color:#0000ff;">in</span><span style="color:#000000;"> a) {
</span><span style="color:#008080;"> 31</span> <span style="color:#000000;"> buildParams(prefix, a[prefix], traditional, add);
</span><span style="color:#008080;"> 32</span> <span style="color:#000000;"> }
</span><span style="color:#008080;"> 33</span> <span style="color:#000000;"> }
</span><span style="color:#008080;"> 34</span>
<span style="color:#008080;"> 35</span> <span style="color:#008000;">//</span><spa |
|