-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajaxform.js
202 lines (177 loc) · 4.59 KB
/
ajaxform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
function:ajax提交表单
version:v1.0
author:weber yang
date:2015.8.8
**/
;(function(window, undefined) {
function form(request, selector) {
return new Form(request, selector);
}
/*
request{url:请求路径, method:请求方法(GET默认,POST...), enctype:表单提交的类型,默认为application/x-www-form-urlencoded}
selector:样式选择器,可以手动过滤表单中需要提交的元素,例如'#form input,select,textarea'仅提交input和select和textarea的元素
*/
var Form = function(request, selector) {
this.selector = selector;
this.init(request);
return this;
};
Form.prototype.init = function(req) {
req.method = req.method ? req.method:'get';
req.requestType = req.enctype ? req.enctype : 'application/x-www-form-urlencoded';
this.request = req;
//初始化表单控件存储,解析表单的时候会把input这样的控件放到这里
this.inputs = {
radios: new Array(),
checkboxs: new Array(),
files: new Array(),
others: new Array(),
};
this.selects = new Array();
this.textareas = new Array();
};
//解析表单
Form.prototype.parseForm = function() {
var elements = document.querySelectorAll(this.selector);
for(var i=0; i<elements.length; i++) {
var ele = elements[i];
switch(ele.nodeName) {
case 'INPUT':
this._handleInput(ele);
break;
case 'SELECT':
this._handleSelect(ele);
break;
case 'TEXTAREA':
this._handleTextarea(ele);
break;
default:break;
}
}
}
Form.prototype._handleInput = function(input) {
if(input.type == 'reset' || input.type == 'submit' || input.type == 'button') {
return ;
}
var inputs = this.inputs;
switch(input.type) {
case 'radio':
if(input.checked) {
inputs.radios.push(input);
}
break;
case 'checkbox':
if(!input.checked) {
break;
}
var checkboxs = inputs.checkboxs;
var checkbox = null;
for(var i=0; i<checkboxs.length;i++) {
if(checkboxs[i].name == input.name) {
checkbox = checkboxs[i];
break;
}
}
if(checkbox != null) {
checkbox.values = checkbox.values + ',' + input.value;
}
else {
input.values = input.value;
inputs.checkboxs.push(input);
}
break;
case 'file':
inputs.files.push(input);
break;
default:
inputs.others.push(input);
break;
}
};
Form.prototype._handleSelect = function(select) {
var selects = this.selects;
if(select.multiple) {
var options = select.options;
var values = '';
for(var i=0; i<options.length; i++) {
if(options[i].selected) {
values = values + ',' + options[i].value;
}
}
select.values = values;
}
selects.push(select);
};
Form.prototype._handleTextarea = function(textarea) {
var textareas = this.textareas;
textareas.push(textarea);
};
//序列化前面解析出来的表单元素
Form.prototype.serialize = function(requestType) {
var result = {};
var i = 0;
//1.input
var radios = this.inputs.radios;
for(i=0; i<radios.length; i++) {
result[radios[i].name] = radios[i].value;
}
var checkboxs = this.inputs.checkboxs;
for(i=0; i<checkboxs.length; i++) {
result[checkboxs[i].name] = checkboxs[i].values;
}
var others = this.inputs.others;
for(i=0; i<others.length; i++) {
result[others[i].name] = others[i].value;
}
//2.select
var selects = this.selects;
for(i=0; i<selects.length; i++) {
if(!selects[i].multiple) {
result[selects[i].name] = selects[i].value;
}
else {
result[selects[i].name] = selects[i].values;
}
}
//3.textarea
var textareas = this.textareas;
for(i=0; i<textareas.length; i++) {
result[textareas[i].name] = textareas[i].value;
}
if(this.inputs.files.length != 0 || requestType == 'multipart/form-data') {
var fd = new FormData();
var files = this.inputs.files;
for(i=0; i<files.length; i++) {
fd.append(files[i]);
}
for(var key in result) {
fd.append(key, result[key]);
}
result = fd;
}
return result;
};
//发送请求,这里使用自己封装的ajax,详见本博客的ajax文章.
Form.prototype.send = function() {
var params = this.serialize(this.request.requestType);
if(params instanceof Blob) {
this.request.requestType = 'multipart/form-data';
}
this.request.params = params;
console.log(params);
ajax(
this.request,
{
success:function(result) {
alert(result)
},
}).send();
};
//手动提交表单
Form.prototype.submit = function() {
this.parseForm();
this.send();
};
window.form = form;
})(window);