Created
October 14, 2008 20:01
-
-
Save kangax/16780 to your computer and use it in GitHub Desktop.
This file contains 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
var serializeElements = (function(){ | |
function isSuccessful(element) { | |
// http://www.w3.org/TR/html401/interact/forms.html#h-17.13.2 | |
return ( | |
!element.disabled && // should not be disabled | |
element.name && // should have a name | |
!'^file|reset$'.test(element.type) && // should not be of `file` or `reset` types | |
Element.getValue(element) != null // should have a non-null value | |
); | |
} | |
return function(elements, options) { | |
if (typeof options != 'object') { | |
options = { hash: !!options }; | |
} | |
else if (Object.isUndefined(options.hash)) { | |
options.hash = true; | |
} | |
var submitted = false, | |
submit = options.submit; | |
var data = elements.inject([], function(data, element) { | |
if (isSuccessful(element) && (element.type != 'submit' || (!submitted && | |
// TODO: this is confusing | |
submit !== false && (!submit || element.name == submit) && (submitted = true)))) { | |
var temp = {}; | |
o[element.name] = Element.getValue(element); | |
data.push(o); | |
} | |
return data; | |
}); | |
return options.hash ? data : Object.toQueryString(data); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment