<form class="form-horizontal" role="form" id="myform" action="" method="post"> <div class="form-group"> <label for="email" class="col-sm-2 control-label">邮箱</label> <div class="col-sm-10"> <input type="email" class="form-control" id="email" name="user[email]" placeholder="Email" value="xxxx.qq.com"> </div> </div> <div class="form-group"> <label for="email" class="col-sm-2 control-label">自我评价</label> <div class="col-sm-10"> <textarea class="form-control" name="user[intro]" rows="3"></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default" id="submit”">提交</button> </div> </div> </form>
把每个表单元素定义为如name="user[xx]"数组的格式,这样方便表单的值序列化 $(function(){ $(document).on('click', '#submit', function(event) { event.preventDefault(); var json_data = $('#myform').serializeJSON(); $.post('post.php', json_data, function(data) { console.log(data); }); }); });
返回的值: POST提交JSON数据:{"user":{"email":"xxxx.qq.com","skill":["javascript","PHP","MySQL"],"salary":"5000","intro":".........."}} PHP处理返回数组:Array ( [user] => Array ( [email] => xxxx.qq.com [skill] => Array ( [0] => javascript [1] => PHP [2] => MySQL ) [salary] => 5000 [intro] => .......... ) )
jquery.serialize-object.js下载:jquery.serialize-object.min.zip