serializeArray()與 serialize()

serialize() 方法通過序列化表單值,創建 URL 編碼文本字符串。可以選擇一個或多個表單元素(比如 input 及/或文本框),或者 form 元素本身。序列化的值可在生成 AJAX 請求時用於 URL 查詢字符串中。舉例:使用ajax時,常常需要拼裝input數據爲'name=xiaoming&sex=1'這種形式,用JQuery的serialize方法可以輕鬆的完成這個工作。

 

serializeArray()序列化表元素 (類似 '.serialize()' 方法) 返回 JSON 數據結構數據。 此方法返回的是JSON對象而非JSON字符串。需要使用插件或者第三方庫進行字符串化操作。

 

jquery.json插件(點我下載)

使用serializeArray( ) 配合 $.toJSON 方法, 我們可以很方便的獲取表單對象的JSON, 並且轉換爲JSON字符串

 

 

 

客戶端代碼:

[html] view plaincopyprint?

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JquerySerialize.aspx.cs"  

  2.     Inherits="JqueryAjaxTest.JquerySerialize" %>  

  3.   

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

  5. <html xmlns="http://www.w3.org/1999/xhtml">  

  6. <head runat="server">  

  7.     <title>Jquery Ajax Test</title>  

  8.     <%--引入Jquery庫--%>  

  9.     <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>  

  10.     <%--引入用於處理Json的插件--%>  

  11.     <script src="Scripts/jquery.json-2.3.min.js" type="text/javascript"></script>  

  12.     <script type="text/javascript">  

  13.   

  14.         $(document).ready(function () {  

  15.             $("#submit").click(function () {  

  16.                 //序列表單內容爲字符串,綁定到DIV.result1  

  17.                 $("#result1").append("<tt>" + $("form").serialize() + "</tt>");  

  18.   

  19.                 //系列化表單元素爲Json對象  

  20.                 var Jsonfields = $("select,:text, :radio,:checkbox").serializeArray();  

  21.                 //將Json對象綁定到Div.result2  

  22.                 $("#result2").append($.toJSON(Jsonfields));  

  23.                 //處理Json對象轉化爲字符串綁定到DIV.result3  

  24.                 jQuery.each(Jsonfields, function (i, field) {  

  25.                     $("#result3").append(field.name + ":" + field.value + " ");  

  26.                 });  

  27.   

  28.   

  29.                 //序列表單內容爲字符串,傳遞到服務器,返回結果綁定到Div.result4  

  30.                 var Jsonform = $("form").serialize();  

  31.                 $.ajax({  

  32.                     url: "Data/SubmitForm.aspx",  

  33.                     type: "get",  

  34.                     data: Jsonform,  

  35.                     success: function (data) {  

  36.                         $("#result4").html(data);  

  37.                     }  

  38.                 });  

  39.   

  40.                 //給結果加上紅色邊框  

  41.                 $("#result").css("border", "1px solid red");  

  42.             });  

  43.         });  

  44.     </script>  

  45. </head>  

  46. <body>  

  47.     <form>  

  48.         <div>  

  49.             <select name="single">  

  50.                 <option selected="selected">Single1</option>  

  51.                 <option>Single2</option>  

  52.             </select>  

  53.         </div>  

  54.         <div>  

  55.             <input type="text" name="txt" value="txt1" /></div>  

  56.         <div>  

  57.             <input type="hidden" name="hid" value="hid1" /></div>  

  58.         <div>  

  59.             <textarea name="txtarea" rows="3" cols="40">txtarea</textarea>  

  60.         </div>  

  61.         <div>  

  62.             <input type="checkbox" name="check" value="check1" checked="checked" />  

  63.             check1  

  64.             <input type="checkbox" name="check" value="check2" />  

  65.             check2</div>  

  66.         <div>  

  67.             <input type="radio" name="radio" value="radio1" checked="checked" />  

  68.             radio1  

  69.             <input type="radio" name="radio" value="radio2" />  

  70.             radio2</div>  

  71.         <div>  

  72.             <input type="button" id="submit" name="sub" value="顯示結果" />  

  73.         </div>  

  74.     </from>  

  75.     <div id="result">  

  76.         <div id="result1">  

  77.             serialize表單的結果爲:  

  78.         </div>  

  79.         <div id="result2">  

  80.             serializeArray返回Json對象:  

  81.         </div>  

  82.         <div id="result3">  

  83.             serializeArray返回Json對象經過處理得到的字符串:  

  84.         </div>  

  85.         <div id="result4">  

  86.         </div>  

  87.     </div>  

  88. </body>  

  89. </html>  

 

服務端代碼:

[csharp] view plaincopyprint?

  1. using System;  

  2. using System.Collections.Generic;  

  3. using System.Linq;  

  4. using System.Web;  

  5. using System.Web.UI;  

  6. using System.Web.UI.WebControls;  

  7.   

  8. namespace JqueryAjaxTest  

  9. {  

  10.     public partial class SubmitForm : System.Web.UI.Page  

  11.     {  

  12.         protected void Page_Load(object sender, EventArgs e)  

  13.         {  

  14.             string single=HttpContext.Current.Request["single"];  

  15.             string txt=HttpContext.Current.Request["txt"];  

  16.             string hid=HttpContext.Current.Request["hid"];  

  17.             string txtarea=HttpContext.Current.Request["txtarea"];  

  18.             string check=HttpContext.Current.Request["check"];  

  19.             string radio=HttpContext.Current.Request["radio"];  

  20.   

  21.             Response.Clear();  

  22.             Response.Write("服務器接收到的表單鍵值對爲:</br>" + "single/" + single + "," + "txt/" +  

  23.                 txt + "," + "hid/" + hid + "," + "txtarea/" + txtarea + "," + "check/" + check + "," + "radio/" + radio);  

  24.             Response.End();  

  25.         }  

  26.     }  

  27. }  


 

注意:標籤名字不能使用js關鍵字,否則很可能出現衝突,導致返回數據爲空。

轉自:http://blog.csdn.net/shan9liang/article/details/7868162

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章