weixin_33709590 2016-05-20 15:03 采纳率: 0%
浏览 57

提交表单数据为JSON

I have to recreate a form that was originally created using Jotform - it is here. I am struggling in one area. It is a pretty simple form, the only caveat being the ability to duplicate a form field form multiple entries. That part can be seen here. When the form is duplicated, I need to submit the form data as a JSON array. In the fiddle, I didn't put the regular form fields, here is how they and the cloned field need to submit the data.

q2_fullName[first]:test
q2_fullName[last]:test
q1_currentCommission1:[{"Instruments":"a","Commissions":"1","Margins":"a"},{"Instruments":"b","Commissions":"2","Margins":"b"},{"Instruments":"c","Commissions":"3","Margins":"c"}]

normally in my $.ajax handler, I just serialize the data, but that doesn't work in creating the json array for the cloned fields. Normally like so:

submitHandler: function(form) {
  var dataString = $(form).serialize(); 
  $.ajax({
    type:'POST',
    url: form.action,
    data: dataString,
    dataType: "json",
    beforeSend: function(data){
     //before send
    },
    success: function(data){
      //success function
    }
  });
  return false;
},

I need to somehow serialize the non cloned fields (I think) and create a json array out of the cloned values and assign them a key name

  • 写回答

1条回答 默认 最新

  • weixin_33693070 2016-05-20 15:29
    关注

    You can build the post data and the json string like this :

    var data = {
          // get/set the firstname etc
          "q2_fullName":{
              "first":"", // get firstname ie $("#first_2").val(),
              "last":"" 
          },
          "q1_currentCommission1" :""
        },
        commisions = [];
    
    $('.InsContain').each(function() {
      var $inputs = $(this).find('input');
      commisions.push({
        "Instruments" : $inputs.eq(0).val(),
        "Commissions" : $inputs.eq(1).val(),
        "Margins" : $inputs.eq(2).val()
      });
    });
    data.q1_currentCommission1 = JSON.stringify(commisions);
    

    Posted data :

    q2_fullName[first]:aaa
    q2_fullName[last]:123
    q1_currentCommission1:[{"Instruments":"1","Commissions":"1","Margins":"1"}]
    

    Update fiddle here

    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系