js將form內的表單序列化為json字串、陣列、物件
阿新 • • 發佈:2019-01-28
- 序列化為字串
var params1 = $(#myform).serialize();//name=zhangsan&sex=1&age=20
- 序列化為陣列
$(#myform).serializeArray();//[Object, Object, Object]
- 序列化為物件
//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 || ''; } }); return o; };