-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjquery.serialize-hash.js
More file actions
52 lines (49 loc) · 1.82 KB
/
jquery.serialize-hash.js
File metadata and controls
52 lines (49 loc) · 1.82 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
(function($){
$.fn.serializeHash = function() {
var hash = {};
/***
JQuery plugin that returns a hash from serialization of any form or dom element. It supports Brackets on input names.
It is convenient if you want to get values from a form and merge it with an other hash for example.
** Added by rilinor on 29/05/2012 : jquery serialize hash now support serialization of any dom elements (before, only form were supported). Thanks !
Example:
---------- HTML ----------
<form id="form">
<input type="hidden" name="firstkey" value="val1" />
<input type="hidden" name="secondkey[0]" value="val2" />
<input type="hidden" name="secondkey[1]" value="val3" />
<input type="hidden" name="secondkey[key]" value="val4" />
</form>
---------- JS ----------
$('#form').serializeHash()
should return :
{
firstkey: 'val1',
secondkey: {
0: 'val2',
1: 'val3',
key: 'val4'
}
}
***/
function stringKey(key, value) {
var beginBracket = key.lastIndexOf('[');
if (beginBracket == -1) {
var hash = {};
hash[key] = value;
return hash;
}
var newKey = key.substr(0, beginBracket);
var newValue = {};
newValue[key.substring(beginBracket + 1, key.length - 1)] = value;
return stringKey(newKey, newValue);
}
var els = $(this).find(':input').get();
$.each(els, function() {
if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /hidden|text|search|tel|url|email|password|datetime|date|month|week|time|datetime-local|number|range|color/i.test(this.type))) {
var val = $(this).val();
$.extend(true, hash, stringKey(this.name, val));
}
});
return hash;
};
})(jQuery);