Form To Json Data
1:
2: //程式碼
3: $.extend($.fn.form.methods, {
4: serialize: function (jq) {
5: var arrayValue = $(jq[0]).serializeArray();
6: var json = {};
7: $.each(arrayValue, function () {
8: var item = this;
9: if (item["name"] != '__VIEWSTATE' && item["name"] != '__EVENTVALIDATION') { //排除 ASP.NET 的 ViewState
10: if (json[item["name"]]) {
11: json[item["name"]] = json[item["name"]] + "," + item["value"];
12: } else {
13: json[item["name"]] = item["value"]== '' ? null : item["value"];
14: }
15: }
16: });
17: return json;
18: },
19: getValue: function (jq, name) {
20: var jsonValue = $(jq[0]).form("serialize");
21: return jsonValue[name];
22: },
23: setValue: function (jq, name, value) {
24: return jq.each(function () {
25: _b(this, _29);
26: var data = {};
27: data[name] = value;
28: $(this).form("load", data);
29: });
30: }
31: });
32:
33: //使用方法:
34: $('form').form('serialize'); //getJson
35: $('form').form('getValue','a'); //getKeyValue
36: $('form').form('setValue',{'name':'a'}); //SetKeyValue