Author: Oswei email:ikmb@163.com reprint Annotated author
Note: 1, JS according to the Table element class attribute, the form element name and value are combined into JSON format, the table cell element class attribute can be targeted to combine JSON data.
2. The back-end asp.net is deserialized into the object's real column with JavaScriptSerializer.
3, Benefits: simplifies the front-end data reading and the back-end data assignment.
Copy Code code as follows:
function Getjsonstr (class_name) {
var a = [];
text box
$ ("." + class_name). Filter (": Text"). each (function (i) {
alert (this.name);
alert (this.value);
A.push ({name:this.name, value:this.value});
});
Drop down list
$ ("." + class_name). Filter ("select"). each (function (i) {
alert (this.name);
alert (this.value);
A.push ({name:this.name, value:this.value});
});
Single selection box
$ ("." + class_name). Filter (": Radio"). Filter (": Checked"). each (function (i) {
alert (this.name);
alert (this.value);
A.push ({name:this.name, value:this.value});
});
check box start
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});
}
check box end
Group to 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
Copy Code code as follows:
Public partial class Test:System.Web.UI.Page
{
protected void Pag E_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;
}