源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Javascript 详解封装from表单数据为json串进行ajax提交

  • 时间:2021-09-25 18:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Javascript 详解封装from表单数据为json串进行ajax提交
[b]摘要: js封装from表单数据为json串进行ajax提交[/b] json封装代码
function getFormJson(frm) { //frm:form表单的id
    var o = {}; 
    var a = $("#"+frm).serializeArray(); 
    $.each(a, function() { 
      if (o[this.name] !== undefined) { 
        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格式,ajax使用如下:
$.ajax({
  type: 'post',
  url: 'your url',
  data: getFormJson(frm),
  success: function(data) {
    // your code
  }
});


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部