1 功能扩展jquery方法,可以将form中数据转换为json格式
2 使用$('#form1').serializeObject()
3 如果将JSON中数据直接转为字符串
JSON.stringify( $('#form1').serializeObject() )
4 json 字符串转换为json对象
var str = '{"name":"小明","age":"23"}' var jsonObj = JSON.parse(str);
form转为JSON
$.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 || ''; } }); return o; }