Created
September 6, 2011 17:35
-
-
Save wesen/1198341 to your computer and use it in GitHub Desktop.
get form data as json
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
| /** | |
| * Extract the value of all form fields specified in fields. | |
| **/ | |
| $.fn.getFormData = function (fields) { | |
| var res = {}; | |
| for (var i = 0; i < fields.length; i++) { | |
| var field = fields[i]; | |
| res[field] = $(this).find("[name=" + field + "]").val(); | |
| } | |
| return res; | |
| }; | |
| /** | |
| * Returns all the input fields of the form. | |
| **/ | |
| $.fn.getAllFormData = function () { | |
| var res = {}; | |
| $(this).find("input, textarea, select, checkbox").each(function (idx, elt) { | |
| elt = $(elt); | |
| res[elt.attr("name")] = elt.val(); | |
| }); | |
| return res; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment