Last active
August 10, 2016 09:52
-
-
Save image72/8ca045f7a956f965052db1422f7d9fae to your computer and use it in GitHub Desktop.
serializeArray from to array. via https://plainjs.com/javascript/ajax/serialize-form-data-into-an-array-46/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function serialize(form) { | |
| var field, l, s = []; | |
| if (typeof form == 'object' && form.nodeName == "FORM") { | |
| var len = form.elements.length; | |
| for (var i=0; i<len; i++) { | |
| field = form.elements[i]; | |
| if (field.name && !field.disabled && field.type != 'file' && field.type != 'reset' && field.type != 'submit' && field.type != 'button') { | |
| if (field.type == 'select-multiple') { | |
| l = form.elements[i].options.length; | |
| for (var j=0; j<l; j++) { | |
| if(field.options[j].selected) | |
| s[s.length] = encodeURIComponent(field.name) + "=" + encodeURIComponent(field.options[j].value); | |
| } | |
| } else if ((field.type != 'checkbox' && field.type != 'radio') || field.checked) { | |
| s[s.length] = encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value); | |
| } | |
| } | |
| } | |
| } | |
| return s.join('&').replace(/%20/g, '+'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function serializeArray(form) { | |
| var field, l, s = []; | |
| if (typeof form == 'object' && form.nodeName == "FORM") { | |
| var len = form.elements.length; | |
| for (i=0; i<len; i++) { | |
| field = form.elements[i]; | |
| if (field.name && !field.disabled && field.type != 'file' && field.type != 'reset' && field.type != 'submit' && field.type != 'button') { | |
| if (field.type == 'select-multiple') { | |
| l = form.elements[i].options.length; | |
| for (j=0; j<l; j++) { | |
| if(field.options[j].selected) | |
| s[s.length] = { name: field.name, value: field.options[j].value }; | |
| } | |
| } else if ((field.type != 'checkbox' && field.type != 'radio') || field.checked) { | |
| s[s.length] = { name: field.name, value: field.value }; | |
| } | |
| } | |
| } | |
| } | |
| return s; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // via zeptojs | |
| var serializeArray = function(form) { | |
| var name, type, result = [], | |
| add = function(value) { | |
| if (value.forEach) return value.forEach(add) | |
| result.push({ name: name, value: value }) | |
| } | |
| if (typeof form == 'object' && form.nodeName == 'FORM') { | |
| [].forEach.call(form.elements, function(field){ | |
| type = field.type, name = field.name | |
| if (name && field.nodeName.toLowerCase() != 'fieldset' && | |
| !field.disabled && type != 'submit' && type != 'reset' && type != 'button' && type != 'file' && | |
| ((type != 'radio' && type != 'checkbox') || field.checked)) | |
| add(field.value) | |
| }) | |
| } | |
| return result | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| serializeJSON = function(form) { | |
| var json = {}; | |
| [].forEach.call(form.querySelectorAll('input,select,textarea'), function(field) { | |
| var el = field, | |
| key = el.name, | |
| val = el.value; | |
| var validElem = val != '' && val !== undefined && val !== null && el.hasAttribute('name') && !el.disabled; | |
| if (validElem) { | |
| if (el.hasAttribute('type') && el.getAttribute('type').toLowerCase() == 'checkbox') { | |
| el.checked && ( (Object.prototype.toString.call(json[key]) == '[object Array]' ) ? json[key].push(val) : json[key] = [val]); | |
| } else if (el.hasAttribute('type') && el.getAttribute('type').toLowerCase() == 'radio') { | |
| el.checked && (json[key] = val); | |
| } else { | |
| json[key] = val; | |
| } | |
| } | |
| }); | |
| return json; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment