Skip to content

Instantly share code, notes, and snippets.

@nissuk
Created February 19, 2011 18:40
Show Gist options
  • Select an option

  • Save nissuk/835256 to your computer and use it in GitHub Desktop.

Select an option

Save nissuk/835256 to your computer and use it in GitHub Desktop.
フォームの入力値をデシリアライズするjQueryプラグイン
/**
* 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