function objectifyForm(formArray) {//serialize data functionvar returnArray = {};for (var i = 0; i < formArray.length; i++){returnArray[formArray[i]['name']] = formArray[i]['value'];}return returnArray;}
function serializeFormObject(form){function trim(str){return str.replace(/^\s+|\s+$/g,"");}
var o = {};var a = $(form).serializeArray();$.each(a, function() {var nameParts = this.name.split('[');if (nameParts.length == 1) {// New value is not an array - so we simply add the new// value to the result objectif (o[this.name] !== undefined) {if (!o[this.name].push) {o[this.name] = [o[this.name]];}o[this.name].push(this.value || '');} else {o[this.name] = this.value || '';}}else {// New value is an array - we need to merge it into the// existing result object$.each(nameParts, function (index) {nameParts[index] = this.replace(/\]$/, '');});
// This $.each merges the new value in, part by partvar arrItem = this;var temp = o;$.each(nameParts, function (index) {var next;var nextNamePart;if (index >= nameParts.length - 1)next = arrItem.value || '';else {nextNamePart = nameParts[index + 1];if (trim(this) != '' && temp[this] !== undefined)next = temp[this];else {if (trim(nextNamePart) == '')next = [];elsenext = {};}}
if (trim(this) == '') {temp.push(next);} elsetemp[this] = next;
temp = next;});}});return o;}
var getFormData = function(form) {//Ignore the submit buttonvar elements = Array.prototype.filter.call(form.elements, function(element) {var type = element.getAttribute('type');return !type || type.toLowerCase() !== 'submit';});
你可以像这样使用它:
function() {
var getFormData = function(form) {//Ignore the submit buttonvar elements = Array.prototype.filter.call(form.elements, function(element) {var type = element.getAttribute('type');return !type || type.toLowerCase() !== 'submit';});
//Make an object out of the form data: {name: value}var data = elements.reduce(function(data, element) {data[element.name] = element.value;return data;}, {});
return data;};
var post = function(action, data, callback) {var request = new XMLHttpRequest();request.onload = callback;request.open('post', action);request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");request.send(JSON.stringify(data), true);request.send();};
var submit = function(e) {e.preventDefault();var form = e.target;var action = form.action;var data = getFormData(form);//change the third argument in order to do something//more intersting with the response than just print itpost(action, data, console.log.bind(console));}
//change formName belowdocument.formName.onsubmit = submit;
})();
/*** serialized a form to a json object** @usage: $("#myform").jsonSerialize();**/
(function($) {"use strict";$.fn.jsonSerialize = function() {var json = {};var array = $(this).serializeArray();$.each(array, function(key, obj) {var value = (obj.value == "") ? false : obj.value;if(value) {// check if we have a numbervar isNum = /^\d+$/.test(value);if(isNum) value = parseFloat(value);// check if we have a booleanvar isBool = /^(false|true)+$/.test(value);if(isBool) value = (value!=="false");}json[obj.name] = value;});return json;}})(jQuery);