简体   繁体   English

将JSON数据输入HTML表单字段

[英]Feed JSON data into HTML form fields

I have some JSON: 我有一些JSON:

"accessories":{
  "tableware01":{
     "sku":"tableware01",
     "forceAmountHidden":"1",
     "upsell":"1",
     "discountAll":"1",
     "forceRemove":"1",
     "percentageDiscount":"12",
     "fixedDiscount":"1",
     "forceAmount":"1",
     "maximumQuantity":"12",
     "endDate":"2014-12-12T00:00:00",
     "minimumQuantity":"1",
     "startDate":"2012-12-12T00:00:00"
  },
  "cla000":{
     "sku":"cla000",
     "forceAmountHidden":"1",
     "upsell":"1",
     "discountAll":"1",
     "forceRemove":"1",
     "percentageDiscount":"23",
     "fixedDiscount":"1",
     "forceAmount":"1",
     "maximumQuantity":"123",
     "endDate":"2015-02-03T00:00:00",
     "minimumQuantity":"1",
     "startDate":"2011-02-03T00:00:00"
  }
   }

I need to push this data into a form. 我需要将此数据推送到表单中。 The fields that I need to generate must look like this: 我需要生成的字段必须如下所示:

<input type="hidden" name="accsku1">
<input type="hidden" name="accdisc1">
<input type="hidden" name="accperc1">
<input type="hidden" name="accreg1">
<input type="hidden" name="accupsell1">
<input type="hidden" name="accnum">
<input type="hidden" name="acclimit1">
<input type="hidden" name="accforce1">
<input type="hidden" name="accforcehid1">
<input type="hidden" name="accforcerm1">

I cannot control the key in the JSON and I cannot control the name of the form field - these need to remain. 我无法控制JSON中的键,也无法控制表单字段的名称-这些需要保留。 I know which fields correspond to which keys in the JSON ie 'accdisc' is 'fixedDiscount'. 我知道哪些字段对应于JSON中的哪些键,即'accdisc'是'fixedDiscount'。 I need to insert the correct value from the JSON into the value of the field. 我需要将JSON中的正确值插入字段的值。 Also, the fields iterate (starting from 1) according to the number of accessories there are ie my form will continue with 另外,字段会根据附件的数量进行迭代(从1开始),即我的表单将继续

<input type="hidden" name="accsku2">
<input type="hidden" name="accdisc2">

and so on. 等等。

I'm quite new to working with JSON but I've been getting through this project ok up to this point where I'm just not sure how to approach this. 我刚开始使用JSON,但是到目前为止,我一直在通过这个项目,直到我不确定该如何处理为止。 I would like to be able to generate the fields as I will not know how many accessories I'll have. 我希望能够生成这些字段,因为我不知道会有多少配件。 I was wondering if I could create some sort of map where I can say 'sku matches input accsku' then add an index? 我想知道是否可以创建某种地图,说“ sku匹配输入accsku”,然后添加索引?

The JSON is a snippet of a larger file containing a lot of product data with varying keys, this is an example of how I am pulling it into my form: JSON是一个较大文件的摘录,其中包含许多具有不同键的产品数据,这是如何将其提取到表单中的一个示例:

$.getJSON("urltoJSONhere",function(product){
    $.each(product.extendedFields, function(i){
        $( "<input type='hidden' name='"+this.name+"' value='"+this.data+"'>" ).appendTo( form );
    });

Above is much simpler for 'extendedFields' as I can use keys 'name' and 'data' to generate the input fields, but I really don't know how to approach my problem above with 'accessories'. 上面的“ extendedFields”要简单得多,因为我可以使用键“ name”和“ data”来生成输入字段,但是我真的不知道如何用“ accessories”解决上面的问题。

Can anyone give me some advice on how I might approach this please? 谁能给我一些建议,以解决我的问题?

Additional info for clarification: 其他说明信息:

I know this: sku = accsku, fixedDiscount = accdisc, percentageDiscount = accperc, discountAll = accreg, upsell = accupsell, minimumQuantity = accnum, maximumQuantity = acclimit, forceAmount = accforce, forceAmountHidden = accforcehid, forceRemove = accforcerm 我知道这一点:sku = accsku,fixedDiscount = accdisc,percentDiscount = accperc,DiscountAll = accreg,加售= Accusell,minimumQuantity = accnum,maximumQuantity = acclimit,forceAmount = accforce,forceAmountHidden = accforcehid,forceRemove = accforcerm

I need this in my form: 我需要我的表格:

<input type="hidden" name="accsku1" value="tableware01">
<input type="hidden" name="accdisc1" value="1">
<input type="hidden" name="accperc1" value="12">
<input type="hidden" name="accreg1" value="1">
<input type="hidden" name="accupsell1" value="1">
<input type="hidden" name="accnum1" value="1">
<input type="hidden" name="acclimit1" value="12">
<input type="hidden" name="accforce1" value="1">
<input type="hidden" name="accforcehid1" value="1">
<input type="hidden" name="accforcerm1" value="1">
<input type="hidden" name="accsku2" value="cla000">
<input type="hidden" name="accdisc2" value="1">
<input type="hidden" name="accperc2" value="23">
<input type="hidden" name="accreg2" value="1">
<input type="hidden" name="accupsell2" value="1">
<input type="hidden" name="accnum2" value="1">
<input type="hidden" name="acclimit2" value="123">
<input type="hidden" name="accforce2" value="1">
<input type="hidden" name="accforcehid2" value="1">
<input type="hidden" name="accforcerm2" value="1">

Check this link if it helps. 检查此链接是否有帮助。

You need to use nested .each to access inner data. 您需要使用嵌套的.each来访问内部数据。

http://jsfiddle.net/pE6yE/9/ http://jsfiddle.net/pE6yE/9/

I've had some help from a work colleague who gave me some assistance creating a map which is then used to build the input fields. 我从一个工作同事那里得到了一些帮助,他给了我一些帮助来创建地图,然后将其用于构建输入字段。 I'm posting this in case anyone else needs a similar solution. 如果有人需要类似的解决方案,我会发布此信息。 The script works with a form with id 'accform'. 该脚本可用于ID为“ accform”的表单。

var accessories = {
"tableware01": {
    "sku": "tableware01",
        "forceAmountHidden": "1",
        "upsell": "1",
        "discountAll": "1",
        "forceRemove": "1",
        "percentageDiscount": "12",
        "fixedDiscount": "1",
        "forceAmount": "1",
        "maximumQuantity": "12",
        "endDate": "2014-12-12T00:00:00",
        "minimumQuantity": "1",
        "startDate": "2012-12-12T00:00:00"
},
    "cla000": {
    "sku": "cla000",
        "forceAmountHidden": "1",
        "upsell": "1",
        "discountAll": "1",
        "forceRemove": "1",
        "percentageDiscount": "23",
        "fixedDiscount": "1",
        "forceAmount": "1",
        "maximumQuantity": "123",
        "endDate": "2015-02-03T00:00:00",
        "minimumQuantity": "1",
        "startDate": "2011-02-03T00:00:00"
}
}

var map = {
    sku: "accsku",
    forceAmountHidden: "accforcehid",
    upsell: "accupsell",
    discountAll: "accreg",
    forceRemove: "accforcerm",
    percentageDiscount: "accperc",
    fixedDiscount: "accdisc",
    forceAmount: "accforce",
    maximumQuantity: "acclimit",
    minimumQuantity: "accnum",
    endDate:"endDate",
    startDate:"startDate"
}
var form = $("#accform");
var index = 1;
for (var k in accessories) {
    var accessory = accessories[k];
    for (var prop in accessory) {
        var name = map[prop] + index;
        $('input[name=' + name + ']').val(name);
        $( "<input type='text' name='"+name+"' value='"+accessory[prop]+"'>" ).appendTo( form );
    }
    index++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM