Created
September 24, 2013 06:34
-
-
Save LeezQ/6681067 to your computer and use it in GitHub Desktop.
用于 form 将 searilized 数据转为 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
| (function($) { | |
| /** | |
| * code from stackoverflow: | |
| * http://stackoverflow.com/questions/1184624/convert-form-data-to-js-object-with-jquery | |
| * | |
| * 用于处理 serializeAray 数据后格式 | |
| * | |
| * <form id="form"> | |
| * <input type="text" name="lname" value="lee"/> | |
| * <input type="text" name="fname" value="zq"/> | |
| * </form> | |
| * | |
| * $('#form').serialize() 返回:lname=lee&fnaem=zq | |
| * | |
| * $('#form').serializeArray() 返回:[{name: lname, value: lee}] | |
| * | |
| * 以下方法转成: | |
| * $('#form').serializeObject() 返回:{lname: lee},更方便 json 格式提交 | |
| * | |
| */ | |
| $.fn.serializeObject = function() { | |
| var o = {}; | |
| var a = this.serializeArray(); | |
| $.each(a, function() { | |
| if (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 || ''; | |
| } | |
| }); | |
| return o; | |
| }; | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment