form中数据直接转为json

 

 

 

 1  功能扩展jquery方法,可以将form中数据转换为json格式

 2 使用$('#form1').serializeObject()

 3 如果将JSON中数据直接转为字符串

 

JSON.stringify( $('#form1').serializeObject()  )

 

  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;
}

 

你可能感兴趣的:(json)