Jquery组合form元素为json格式,asp.net反序列化[转]

作者:敖士伟 Email:ikmb@163.com 转载注明作者

说明: 1、js根据表单元素class属性,把表单元素的name和value组合为json格式;用表单元素class属性可以针对性地组合JSON数据。

2、后端ASP.NET用JavaScriptSerializer反序列化为对象实列。

3、好处:简化了前端数据读取与后端数据赋值。

作者:敖士伟 Email:ikmb@163.com 转载注明作者

JS

 

[javascript] view plaincopy
  1. function GetJSONStr(class_name) {  
  2.             var a = [];  
  3.             //文本框  
  4.             $("." + class_name).filter(":text").each(function(i) {  
  5.                 //alert(this.name);  
  6.                 //alert(this.value);  
  7.                 a.push({ name: this.name, value: this.value });  
  8.   
  9.             });  
  10.   
  11.     //多列文本框  
  12.     $("." + class_name).filter("textarea").each(function(i) {  
  13.         //alert(this.name);  
  14.         //alert(this.value);  
  15.         a.push({ name: this.name, value: this.value });  
  16.   
  17.     });  
  18.             //下拉列表  
  19.             $("." + class_name).filter("select").each(function(i) {  
  20.                 //alert(this.name);  
  21.                 //alert(this.value);  
  22.                 a.push({ name: this.name, value: this.value });  
  23.   
  24.             });  
  25.             //单选框  
  26.             $("." + class_name).filter(":radio").filter(":checked").each(function(i) {  
  27.                 //alert(this.name);  
  28.                 //alert(this.value);  
  29.                 a.push({ name: this.name, value: this.value });  
  30.             });  
  31.             //复选框开始  
  32.             var temp_cb = "";  
  33.             $("." + class_name).filter(":checkbox").filter(":checked").each(function(i) {  
  34.                 if (temp_cb.indexOf(this.name) == -1) {  
  35.                     temp_cb += this.name + ",";  
  36.                 }  
  37.   
  38.             });  
  39.             var temp_cb_arr = temp_cb.split(",");  
  40.             var cb_name = "";  
  41.             var cb_value = "";  
  42.             for (var temp_cb_i = 0; temp_cb_i < temp_cb_arr.length - 1; temp_cb_i++) {  
  43.                 cb_name = temp_cb_arr[temp_cb_i];  
  44.                 var cb_value_length = $("input[name='" + temp_cb_arr[temp_cb_i] + "']:checked").length;  
  45.                 $("input[name='" + temp_cb_arr[temp_cb_i] + "']:checked").each(function(i) {  
  46.                     if (i == cb_value_length - 1)  
  47.                         cb_value += this.value;  
  48.                     else  
  49.                         cb_value += this.value + ",";  
  50.   
  51.                 });  
  52.                 //alert(cb_name);  
  53.                 //alert(cb_value);  
  54.                 a.push({ name: cb_name, value: cb_value });  
  55.             }  
  56.             //复选框结束  
  57.   
  58.   
  59.             //组合为JSON  
  60.             var temp_json = "";  
  61.             for (var json_i = 0; json_i < a.length; json_i++) {  
  62.                 if (json_i != a.length - 1) {  
  63.                     temp_json += '"' + a[json_i].name + '":"' + a[json_i].value + '",';  
  64.                 }  
  65.                 else {  
  66.                     temp_json += '"' + a[json_i].name + '":"' + a[json_i].value + '"';  
  67.                 }  
  68.             }  
  69.             return "{" + temp_json + "}";  
  70.         }  

 

ASP.NET

[c-sharp] view plaincopy
  1. public partial class test : System.Web.UI.Page  
  2.     {  
  3.         protected void Page_Load(object sender, EventArgs e)  
  4.         {  
  5.             JavaScriptSerializer Serializer = new JavaScriptSerializer();  
  6.             string r = Request.Form["msg"];  
  7.   
  8.             //{"Name":"MyName1","Single":"one"}  
  9.   
  10.             t_json t_json_object = Serializer.Deserialize<t_json>(r);  
  11.   
  12.             Response.Write(t_json_object.Name);  
  13.             Response.End();  
  14.         }  
  15.     }  
  16.   
  17.     class t_json  
  18.     {  
  19.         public DateTime Name;  
  20.         public string Single;  
  21.     }  

 

作者:敖士伟 Email:ikmb@163.com 转载注明作者2013-06-20

posted @ 2013-06-20 01:02  weikai  阅读(224)  评论(0编辑  收藏  举报