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

    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services