亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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;
}

相關文章

最新評論