Created
February 19, 2011 18:40
-
-
Save nissuk/835256 to your computer and use it in GitHub Desktop.
フォームの入力値をデシリアライズするjQueryプラグイン
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
| /** | |
| * jQuery deserialize plugin | |
| * public domain | |
| * | |
| * 使用例: | |
| * $('form').deserialize('checkbox[]=1&checkbox[]=2&checkbox[]=3'); | |
| * $('form').deserialize(location.search); | |
| */ | |
| (function($){ | |
| $.fn.deserialize = function(s, options){ | |
| var opts = $.extend({}, $.fn.deserialize.defaults, options); | |
| return this.each(function(){ | |
| var self = this; | |
| var parts = s.replace(/^\?/, '').split('&'); | |
| var data = {}; | |
| $.each(parts, function(i, part){ | |
| var pair = part.split('='); | |
| var name = decodeURIComponent(pair[0]); | |
| var value = decodeURIComponent(pair[1]); | |
| if (opts.filter && !opts.filter.call(self, name, value)) return; | |
| if (!(name in data)) data[name] = []; | |
| data[name].push(value); | |
| }); | |
| $.each(data, function(name, values){ | |
| $('[name="' + name + '"]:input', self).val(values); | |
| }); | |
| }); | |
| }; | |
| $.fn.deserialize.defaults = { | |
| filter: function(name, value){ | |
| if ($('[name="' + name + '"]:input', this).is(':submit')) return false; | |
| return true; | |
| } | |
| }; | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment