我使用jQuery $ .get从php中引出一些json数据。它看起来像这样:
[{
"itemNumber_1": "1",
"partNumber_1": "U1234567890-XYZ",
"description_1": "front label",
"artworkFee_1": "35.00",
"expediteDelivery_1": "55.00",
"height_1": "1.27",
"width_1": "5.05",
"edgeColorNumber_1": "3",
"pmsColorNumber_1": "0",
"cutoutNumber_1": "5",
"adhesiveFreeNumber_1": "0",
"clearWindowNumber_1": "0",
"barcodeNumber_1": "0",
"serialNumber_1": "0",
"replaceablesNumber_1": "0",
"yAxisSpacing_1": "0.10",
"xAxisSpacing_1": "0.10",
"borderSpacing_1": "0.10",
"scrapFactor_1": "5.00"
},
{
"itemNumber_2": "2",
"partNumber_2": "1234567890-ABC",
"description_2": "back label",
"artworkFee_2": "0.00",
"expediteDelivery_2": "0.00",
"height_2": "1.25",
"width_2": "4.00",
"edgeColorNumber_2": "2",
"pmsColorNumber_2": "1",
"cutoutNumber_2": "2",
"adhesiveFreeNumber_1": "1",
"clearWindowNumber_2": "1",
"barcodeNumber_1": "0",
"serialNumber_2": "0",
"replaceablesNumber_2": "0",
"yAxisSpacing_2": "0.10",
"xAxisSpacing_2": "0.10",
"borderSpacing_2": "0.10",
"scrapFactor_2": "5.00"
}]
我正在尝试循环它,类似于循环通过二维数组,但是当我使用变量键[i]而不是硬编码数字键[1]时它会失败。
function useExistingQuoteToFillForm(){
$.get('includes/getQuoteSessionVariables.php', function(response){
var js_JSONobject = JSON.parse(response);
$("#1 input[name='width_1']").attr("value", js_JSONobject[0]['width_1'] );
$("#1 input[name='height_1']").attr('value', js_JSONobject[0]['height_1']);
$("#1 input[name='artworkSetup_1']").attr('value', js_JSONobject[0]['artworkSetup_1']);
$("#1 input[name='expedite_1']").attr('value', js_JSONobject[0]['expedite_1']);
$("#1 input[name='partNumber_1']").attr('value', js_JSONobject[0]['partNumber_1']);
$("#1 input[name='description_1']").attr('value', js_JSONobject[0]['description_1']);
$("#1 select[name='lexan_1']" ).val( js_JSONobject[0]['lexan_1'] );
$("#1 select[name='vinyl_1']" ).val( js_JSONobject[0]['vinyl_1'] );
$("#1 select[name='polyester_1']" ).val( js_JSONobject[0]['polyester_1'] );
$("#1 select[name='xAxisSpacing_1']").val(js_JSONobject[0]['xAxisSpacing_1']);
$("#1 select[name='yAxisSpacing_1']").val(js_JSONobject[0]['yAxisSpacing_1']);
$("#1 select[name='borderSpacing_1']").val(js_JSONobject[0]['borderSpacing_1']);
$("#1 select[name='scrapFactor_1']").val(js_JSONobject[0]['scrapFactor_1']);
$("#1 select[name='colors_1']").val(js_JSONobject[0]['edgeColorNumber_1']);
$("#1 select[name='colorMatch_1']").val(js_JSONobject[0]['pmsColorNumber_1']);
$("#1 select[name='cutOut_1']").val(js_JSONobject[0]['cutOutNumber_1']);
$("#1 select[name='seeThru_1']").val(js_JSONobject[0]['seeThruNumber_1']);
$("#1 select[name='barCode_1']").val(js_JSONobject[0]['barCodeNumber_1']);
$("#1 select[name='serialNumbers_1']").val(js_JSONobject[0]['serialNumbers_1']);
$("#1 select[name='replaceable_1']").val(js_JSONobject[0]['replaceablesNumber_1']);
$("#1 select[name='adhesiveFree_1']").val(js_JSONobject[0]['adhesiveFree_1']);
// end panel 1
// if more than one item panel
if ( Object.keys(js_JSONobject).length > 1) {
// loop thru object for item panel data
for(var i = 1; i < Object.keys(js_JSONobject).length; i++){
var y = i + 1;
addItem();
setTimeout( function(){ // needs a small delay while the new panel comes up
// this hardcoded numeric key works
$("#" + y + " input[name='width_" + y + "']").attr("value", js_JSONobject[1]['width_'+ y]);
// when I switch to using a variable key it fails to proceed
$("#" + y + " input[name='height_'" + y + "']").attr('value', js_JSONobject[i]['height_' + y]);
$("#" + y + " input[name='artworkSetup_'" + y + "']").attr('value', js_JSONobject[i]['artworkSetup_' + y]);
$("#" + y + " input[name='expedite_'" + y + "']").attr('value', js_JSONobject[i]['expedite_' + y]);
$("#" + y + " input[name='partNumber_'" + y + "']").attr('value', js_JSONobject[i]['partNumber_' + y]);
$("#" + y + " input[name='description_'" + y + "']").attr('value', js_JSONobject[i]['description_' + y]);
$("#" + y + " select[name='lexan_'" + y + "']").val( js_JSONobject[i]['lexan_' + y]);
$("#" + y + " select[name='vinyl_'" + y + "']").val( js_JSONobject[i]['vinyl_' + y]);
$("#" + y + " select[name='polyester_'" + y + "']").val( js_JSONobject[i]['polyester_' + y]);
$("#" + y + " select[name='xAxisSpacing_'" + y + "']").val(js_JSONobject[i]['xAxisSpacing_' + y]);
$("#" + y + " select[name='yAxisSpacing_'" + y + "']").val(js_JSONobject[i]['yAxisSpacing_' + y]);
$("#" + y + " select[name='borderSpacing_'" + y + "']").val(js_JSONobject[i]['borderSpacing_' + y]);
$("#" + y + " select[name='scrapFactor_'" + y + "']").val(js_JSONobject[i]['scrapFactor_' + y]);
$("#" + y + " select[name='colors_'" + y + "']").val(js_JSONobject[i]['edgeColorNumber_' + y]);
$("#" + y + " select[name='colorMatch_'" + y + "']").val(js_JSONobject[i]['pmsColorNumber_' + y]);
$("#" + y + " select[name='cutOut_'" + y + "']").val(js_JSONobject[i]['cutOutNumber_' + y]);
$("#" + y + " select[name='seeThru_'" + y + "']").val(js_JSONobject[i]['seeThruNumber_' + y]);
$("#" + y + " select[name='barCode_'" + y + "']").val(js_JSONobject[i]['barCodeNumber_' + y]);
$("#" + y + " select[name='serialNumbers_'" + y + "']").val(js_JSONobject[i]['serialNumbers_' + y]);
$("#" + y + " select[name='replaceable_'" + y + "']").val(js_JSONobject[i]['replaceablesNumber_' + y]);
$("#" + y + " select[name='adhesiveFree_'" + y + "']").val(js_JSONobject[i]['adhesiveFree_' + y ]);
}, 3000 );
}
}
});
}
显然,我是新手,我没理解到什么。有人可以帮忙吗?
答案 0 :(得分:1)
嗯......确切的错误信息是什么?感谢信息。
请在setTimeout参数中检查您的函数范围。看来你给setTimeout的函数在其范围内没有vars i,y,js_JSONobject,所以你应该声明
setTimeout ( function(i,y,js_JSONobject) { ... } ,3000 )
答案 1 :(得分:1)
所以,主要问题实际上是在超时内调用函数。它不起作用,因为当执行时间到来时,你的i
变量现在是另一个变量。要使其工作,您应该以不同的方式调用该函数:
...
(function(js_JSONobject, i){ // I add this before timeout.
setTimeout(function(){
... // The rest of your code.
}, 3000);
})(js_JSONobject, i); // and this after timeout.
....