jq将form 表单数据转为object

我平常上传数据都是使用$(this).serialize(),但是这次我需要使用上传数据的对象。结果这个方法给我转成了字符串。特别不方便。于是找到一个插件。就可以实现,获取的时候,获取到的是对象而不是字符串了。

首先,我们先加载jquery;

1
<script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

然后在后面写一个js方法就好了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script type="text/javascript">
$.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;
};
</script>

这样我们直接就可以获取对象了:

1
$(form).serializeObject();

这样我们获取到的数据直接就是对象了。

参考资料: