如何将form内的表单序列化为json字符串
展开全部
序列化为字符串
[javascript] view plain copy
var params1 = $(#myform).serialize();//name=zhangsan&sex=1&age=20
序列化为数组
[javascript] view plain copy
$(#myform).serializeArray();//[Object, Object, Object]
序列化为对象
[javascript] view plain copy
//form表单上数据转成[{name:value},,,,]
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
[javascript] view plain copy
var params1 = $(#myform).serialize();//name=zhangsan&sex=1&age=20
序列化为数组
[javascript] view plain copy
$(#myform).serializeArray();//[Object, Object, Object]
序列化为对象
[javascript] view plain copy
//form表单上数据转成[{name:value},,,,]
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询