-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserializeObject.js
More file actions
89 lines (65 loc) · 2.59 KB
/
Copy pathserializeObject.js
File metadata and controls
89 lines (65 loc) · 2.59 KB
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
/*
* Project: SerializeObject()
* URL: https://github.com/scottyc1000/serializeObject
* Author: Scott Carmichael
* Version: 1.3
* Requires: jQuery 1.3+
*
* Copyright (c) 2013 Scott Carmichael
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* Copyright notice and license must remain intact for legal use
*/
;(function($, window, document){
$.fn.serializeObject = function() {
//Internet Explorer indxOf fix
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) {
return i;
}
}
return -1;
}
}
var form_values = $(this).serializeArray(),
form_final = {};
//fill object with results
$.each(form_values, function(){
//Empty assign Null
if(this.value == '') {
form_final[this.name] = null;
//Store Associated Array Input Array
} else if(this.name.match(/\[(.+?)\]/g)){
var arrayName = this.name.match(/\w+[^\[]/g)[0],
propertyName = this.name.match(/\[(.+?)\]/g)[0];
propertyName = propertyName.replace(/(\[|\])/g, "");
if(!form_final.hasOwnProperty(arrayName)){
form_final[arrayName] = new Object();
}
form_final[arrayName][propertyName] = this.value;
// Store Array Input Array
} else if(this.name.indexOf('[]') > 0){
//Remove [] from input name
this.name = this.name.split("[]")[0];
if(form_final.hasOwnProperty(this.name)){
form_final[this.name].push(this.value);
} else {
form_final[this.name] = [this.value]
}
// Store multiple / checkboxes as Array
} else if(form_final.hasOwnProperty(this.name)) {
if(typeof form_final[this.name] != 'object' ){
firstItem = form_final[this.name];
form_final[this.name] = new Object();
form_final[this.name][firstItem] = true;
}
form_final[this.name][this.value] = true;
} else {
form_final[this.name] = this.value;
}
});
//Output Object
return form_final;
}
}(jQuery, window, document));