我试图创建一个这样的 JSON 对象:
bta.addConversion({ "order_id": "", "date": "", "items": [
{ "item_id":"item1", "desc":"description", "amount":"$123.33", "quantity":"1" },
{ "item_id":"item2", "desc":"description 2", "amount":"$10.25", "quantity":"1" }
]});
使用这些值:
<input id="InvItemId[0]" name="sku" value="M0215508" type="hidden">
<input id="InvItemname[0]" name="CommerceItem" value="4 Seasons Wine Club Whites (RO)" type="hidden">
<input id="InvItemPrice[0]" name="Sku" value="129.95" type="hidden">
<input id="InvItemQty[0]" name="CommerceItem" value="1" type="hidden">
<input id="InvItemId[1]" name="sku" value="C0429210" type="hidden">
<input id="InvItemname[1]" name="CommerceItem" value="Laetitia Vineyard Select Pinot Noir" type="hidden">
<input id="InvItemPrice[1]" name="Sku" value="273.99" type="hidden">
<input id="InvItemQty[1]" name="CommerceItem" value="1" type="hidden">
<input id="InvItemId[2]" name="sku" value="M0249908" type="hidden">
<input id="InvItemname[2]" name="CommerceItem" value="Top-Value American Reds" type="hidden">
<input id="InvItemPrice[2]" name="Sku" value="199.99" type="hidden">
<input id="InvItemQty[2]" name="CommerceItem" value="1" type="hidden">
<input id="InvItemSize" name="/atg/userprofiling/Profile" value="3" type="hidden">
使用 JavaScript,我已经走到了这一步——但感觉有点被破解了。所以想知道是否有更好的方法来做到这一点:
jQuery(document).ready(function() {
var invItemId = $('#InvID').val();
var invItemsize = $('#InvItemSize').val();
var InvItems='';
var InvItemPrices='';
var InvQtys='';
var InvCats='';
for(var itemCount=0;itemCount<invItemsize;itemCount++){
var InvItem =document.getElementById("InvItemId["+itemCount+"]").value ;
var ItemPrice = document.getElementById("InvItemPrice["+itemCount+"]").value ;
var ItemQty = document.getElementById("InvItemQty["+itemCount+"]").value ;
var InvItemName = document.getElementById("InvItemname["+itemCount+"]").value ;
var itemsList='{"item_id":"'+InvItem+'","desc:""'+InvItemName+'", "amount:"'+ItemPrice+'", "quantity":"'+ItemQty+'"},';
console.log(itemsList);
}
});