Jquery 組合form元素為json格式,asp.net反序列化
更新時間:2009年07月09日 23:15:51 作者:
Jquery組合form元素為json格式,asp.net反序列化實現(xiàn)代碼,大家可以具體的看下面的說明。
作者:敖士偉 Email:ikmb@163.com 轉載注明作者
說明: 1、js根據(jù)表單元素class屬性,把表單元素的name和value組合為json格式;用表單元素class屬性可以針對性地組合JSON數(shù)據(jù)。
2、后端ASP.NET用JavaScriptSerializer反序列化為對象實列。
3、好處:簡化了前端數(shù)據(jù)讀取與后端數(shù)據(jù)賦值。
function GetJSONStr(class_name) {
var a = [];
//文本框
$("." + class_name).filter(":text").each(function(i) {
//alert(this.name);
//alert(this.value);
a.push({ name: this.name, value: this.value });
});
//下拉列表
$("." + class_name).filter("select").each(function(i) {
//alert(this.name);
//alert(this.value);
a.push({ name: this.name, value: this.value });
});
//單選框
$("." + class_name).filter(":radio").filter(":checked").each(function(i) {
//alert(this.name);
//alert(this.value);
a.push({ name: this.name, value: this.value });
});
//復選框開始
var temp_cb = "";
$("." + class_name).filter(":checkbox").filter(":checked").each(function(i) {
if (temp_cb.indexOf(this.name) == -1) {
temp_cb += this.name + ",";
}
});
var temp_cb_arr = temp_cb.split(",");
var cb_name = "";
var cb_value = "";
for (var temp_cb_i = 0; temp_cb_i < temp_cb_arr.length - 1; temp_cb_i++) {
cb_name = temp_cb_arr[temp_cb_i];
var cb_value_length = $("input[name='" + temp_cb_arr[temp_cb_i] + "']:checked").length;
$("input[name='" + temp_cb_arr[temp_cb_i] + "']:checked").each(function(i) {
if (i == cb_value_length - 1)
cb_value += this.value;
else
cb_value += this.value + ",";
});
//alert(cb_name);
//alert(cb_value);
a.push({ name: cb_name, value: cb_value });
}
//復選框結束
//組合為JSON
var temp_json = "";
for (var json_i = 0; json_i < a.length; json_i++) {
if (json_i != a.length - 1) {
temp_json += '"' + a[json_i].name + '":"' + a[json_i].value + '",';
}
else {
temp_json += '"' + a[json_i].name + '":"' + a[json_i].value + '"';
}
}
return "{" + temp_json + "}";
}
ASP.NET
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
JavaScriptSerializer Serializer = new JavaScriptSerializer();
string r = Request.Form["msg"];
//{"Name":"MyName1","Single":"one"}
t_json t_json_object = Serializer.Deserialize<t_json>(r);
Response.Write(t_json_object.Name);
Response.End();
}
}
class t_json
{
public DateTime Name;
public string Single;
}
說明: 1、js根據(jù)表單元素class屬性,把表單元素的name和value組合為json格式;用表單元素class屬性可以針對性地組合JSON數(shù)據(jù)。
2、后端ASP.NET用JavaScriptSerializer反序列化為對象實列。
3、好處:簡化了前端數(shù)據(jù)讀取與后端數(shù)據(jù)賦值。
復制代碼 代碼如下:
function GetJSONStr(class_name) {
var a = [];
//文本框
$("." + class_name).filter(":text").each(function(i) {
//alert(this.name);
//alert(this.value);
a.push({ name: this.name, value: this.value });
});
//下拉列表
$("." + class_name).filter("select").each(function(i) {
//alert(this.name);
//alert(this.value);
a.push({ name: this.name, value: this.value });
});
//單選框
$("." + class_name).filter(":radio").filter(":checked").each(function(i) {
//alert(this.name);
//alert(this.value);
a.push({ name: this.name, value: this.value });
});
//復選框開始
var temp_cb = "";
$("." + class_name).filter(":checkbox").filter(":checked").each(function(i) {
if (temp_cb.indexOf(this.name) == -1) {
temp_cb += this.name + ",";
}
});
var temp_cb_arr = temp_cb.split(",");
var cb_name = "";
var cb_value = "";
for (var temp_cb_i = 0; temp_cb_i < temp_cb_arr.length - 1; temp_cb_i++) {
cb_name = temp_cb_arr[temp_cb_i];
var cb_value_length = $("input[name='" + temp_cb_arr[temp_cb_i] + "']:checked").length;
$("input[name='" + temp_cb_arr[temp_cb_i] + "']:checked").each(function(i) {
if (i == cb_value_length - 1)
cb_value += this.value;
else
cb_value += this.value + ",";
});
//alert(cb_name);
//alert(cb_value);
a.push({ name: cb_name, value: cb_value });
}
//復選框結束
//組合為JSON
var temp_json = "";
for (var json_i = 0; json_i < a.length; json_i++) {
if (json_i != a.length - 1) {
temp_json += '"' + a[json_i].name + '":"' + a[json_i].value + '",';
}
else {
temp_json += '"' + a[json_i].name + '":"' + a[json_i].value + '"';
}
}
return "{" + temp_json + "}";
}
ASP.NET
復制代碼 代碼如下:
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
JavaScriptSerializer Serializer = new JavaScriptSerializer();
string r = Request.Form["msg"];
//{"Name":"MyName1","Single":"one"}
t_json t_json_object = Serializer.Deserialize<t_json>(r);
Response.Write(t_json_object.Name);
Response.End();
}
}
class t_json
{
public DateTime Name;
public string Single;
}
相關文章
jQuery動畫效果animate和scrollTop結合使用實例
animate是jq的一個特效的函數(shù)方法,animate() 方法執(zhí)行 CSS 屬性集的自定義動畫。該方法通過CSS樣式將元素從一個狀態(tài)改變?yōu)榱硪粋€狀態(tài)。2014-04-04自編jQuery插件實現(xiàn)模擬alert和confirm
現(xiàn)在絕大多數(shù)網(wǎng)站都不用自帶的alert和confirm了,因為界面太生硬了。因此這個插件就這樣產(chǎn)生了...2014-09-09artDialog 4.1.5 Dreamweaver代碼提示/補全插件 附下載
artDialog是一個輕巧且高度兼容的javascript對話框組件,可讓你的網(wǎng)頁交互擁有桌面軟件般的用戶體驗2012-07-07jQuery創(chuàng)建及操作xml格式數(shù)據(jù)示例
這篇文章主要介紹了jQuery創(chuàng)建及操作xml格式數(shù)據(jù),結合實例形式分析了jQuery針對xml格式數(shù)據(jù)的創(chuàng)建、解析、添加等相關操作技巧,需要的朋友可以參考下2018-05-05jQuery模擬物體自由落體運動(附演示與demo源碼下載)
這篇文章主要介紹了jQuery模擬物體自由落體運動的方法,并附帶了效果演示與demo源碼下載,可真是模擬出球體自由落體運動及動能損耗的運動過程,需要的朋友可以參考下2016-01-01