Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- The serializeHash method can be used to submit forms with an ajax request.
- The result of serializeHash can be used as the 'data' option for jQuery's
- built-in ajax methods and functions.
- ***/
- (function($){
- $.fn.serializeHash = function() {
- var hash = {};
- /***
- Use serializeArray() to get an array of JSON objects for
- each value in the form.
- (As opposed to serialize() which requires string splitting)
- ***/
- $.each(this.serializeArray(), function() {
- if(hash[this.name] === undefined) {
- hash[this.name] = this.value;
- }
- else {
- /***
- Handle form elements with multiple values such as
- <select multiple="multiple"> and multiple
- <input type="checkbox"/> elements with the same name.
- ***/
- if ($.isArray(hash[this.name])) {
- hash[this.name].push(this.value);
- }
- else {
- hash[this.name] = [hash[this.name], this.value];
- }
- }
- });
- return hash;
- };
- })(jQuery);
Add Comment
Please, Sign In to add comment