Author: Yan Shiwei Email: ikmb@163.com reprinted author
Note: 1. js combines the name and value of the form element into JSON format based on the class attribute of the form element. JSON data can be combined in a targeted manner using the class attribute of the form element.
2. the backend ASP. net uses javascriptserializer to deserialize the object into a real column.
3. Benefits: It simplifies front-end Data Reading and backend Data assignment.
CopyCode The Code is 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 region
$ ("." + 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 starts
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 ends
// 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 codeThe Code is as follows: 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;
}