Jquery 組合form元素為json格式,asp.net反序列化
更新時(shí)間:2009年07月09日 23:15:51 作者:
Jquery組合form元素為json格式,asp.net反序列化實(shí)現(xiàn)代碼,大家可以具體的看下面的說(shuō)明。
作者:敖士偉 Email:ikmb@163.com 轉(zhuǎn)載注明作者
說(shuō)明: 1、js根據(jù)表單元素class屬性,把表單元素的name和value組合為json格式;用表單元素class屬性可以針對(duì)性地組合JSON數(shù)據(jù)。
2、后端ASP.NET用JavaScriptSerializer反序列化為對(duì)象實(shí)列。
3、好處:簡(jiǎn)化了前端數(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 });
});
//復(fù)選框開(kāi)始
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 });
}
//復(fù)選框結(jié)束
//組合為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;
}
說(shuō)明: 1、js根據(jù)表單元素class屬性,把表單元素的name和value組合為json格式;用表單元素class屬性可以針對(duì)性地組合JSON數(shù)據(jù)。
2、后端ASP.NET用JavaScriptSerializer反序列化為對(duì)象實(shí)列。
3、好處:簡(jiǎn)化了前端數(shù)據(jù)讀取與后端數(shù)據(jù)賦值。
復(fù)制代碼 代碼如下:
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 });
});
//復(fù)選框開(kāi)始
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 });
}
//復(fù)選框結(jié)束
//組合為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
復(fù)制代碼 代碼如下:
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;
}
相關(guān)文章
jQuery動(dòng)畫(huà)效果animate和scrollTop結(jié)合使用實(shí)例
animate是jq的一個(gè)特效的函數(shù)方法,animate() 方法執(zhí)行 CSS 屬性集的自定義動(dòng)畫(huà)。該方法通過(guò)CSS樣式將元素從一個(gè)狀態(tài)改變?yōu)榱硪粋€(gè)狀態(tài)。2014-04-04自編jQuery插件實(shí)現(xiàn)模擬alert和confirm
現(xiàn)在絕大多數(shù)網(wǎng)站都不用自帶的alert和confirm了,因?yàn)榻缑嫣擦?。因此這個(gè)插件就這樣產(chǎn)生了...2014-09-09jQuery EasyUI Dialog拖不下來(lái)如何解決
這篇文章主要介紹了jQuery EasyUI Dialog拖不下來(lái)到底如何解決,遇到這類問(wèn)題的,或者是感興趣的小朋友可以參考一下2015-09-09artDialog 4.1.5 Dreamweaver代碼提示/補(bǔ)全插件 附下載
artDialog是一個(gè)輕巧且高度兼容的javascript對(duì)話框組件,可讓你的網(wǎng)頁(yè)交互擁有桌面軟件般的用戶體驗(yàn)2012-07-07jQuery創(chuàng)建及操作xml格式數(shù)據(jù)示例
這篇文章主要介紹了jQuery創(chuàng)建及操作xml格式數(shù)據(jù),結(jié)合實(shí)例形式分析了jQuery針對(duì)xml格式數(shù)據(jù)的創(chuàng)建、解析、添加等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05jQuery 下拉列表 二級(jí)聯(lián)動(dòng)插件分享
jQuery二級(jí)聯(lián)動(dòng)插件:jQuery.selected 一個(gè)頁(yè)面可以引用多個(gè)聯(lián)動(dòng)效果2012-03-03jQuery模擬物體自由落體運(yùn)動(dòng)(附演示與demo源碼下載)
這篇文章主要介紹了jQuery模擬物體自由落體運(yùn)動(dòng)的方法,并附帶了效果演示與demo源碼下載,可真是模擬出球體自由落體運(yùn)動(dòng)及動(dòng)能損耗的運(yùn)動(dòng)過(guò)程,需要的朋友可以參考下2016-01-01