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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀