-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializeToObj.js
43 lines (38 loc) · 1.45 KB
/
serializeToObj.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
$.fn.extend({
serializeToObj: function( setting ){
if( !$(this).length ) return;
var opts = $.extend({
splitStr: null,
ignoreIgnoreSet: false,
includeDisabled: false
}, setting);
var data = {};
$(this).find(':input').each(function(){
var name = $(this).attr('name') || this.id,
type = $(this).attr('type');
if( ( ! opts.ignoreIgnoreSet && $(this).data('ignore')) || 'reset-button-submit-file'.indexOf(type) > -1 || ( ! opts.includeDisabled && $(this).is(':disabled')))
return true;
if( name ){
var value = $(this).val();
if( data[name] == undefined ){
if( ! opts.splitStr && type == 'checkbox' ){
data[name] = $(this).is(':checked') ? [value] : [];
} else {
data[name] = value;
}
} else {
if( (type == 'checkbox' || type == 'radio' ) && ! $(this).is(':checked') )
return true;
if( opts.splitStr ){
data[name] += (opts.splitStr + value);
} else {
if( typeof data[name] != 'object' )
data[name] = [data[name]];
else
data[name].push(value);
}
}
});
return data;
}
});