Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在回调函数中正确访问数组值?_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 如何在回调函数中正确访问数组值?

Javascript 如何在回调函数中正确访问数组值?,javascript,jquery,ajax,Javascript,Jquery,Ajax,我试图在循环内的函数中正确访问IDs[I]的值。我试过以下方法 我认为这个方法将ID记录为字符串。我试图用索引访问它,但结果是未定义的。请参阅SimpleWithTrPrice函数调用中的console.log for(i=0; i<IDs.length; i++) { console.log("Outside of function Vendor is " + IDs[i]);//logs correctly var

我试图在循环内的函数中正确访问IDs[I]的值。我试过以下方法

我认为这个方法将ID记录为字符串。我试图用索引访问它,但结果是未定义的。请参阅SimpleWithTrPrice函数调用中的console.log

for(i=0; i<IDs.length; i++)
        {   
            console.log("Outside of function Vendor is " + IDs[i]);//logs correctly
            var optionSelectionArray = currentlySelectedAttributes(IDs[i]);
            simpleWithAttrPrice(optionSelectionArray, function(data) {
                //var vendor = IDs[i];
                var basePrice = parseFloat(roundDollar(data));
                //newPriceArray[vendor][colorSelected]=basePrice;
                console.log("Vendor is " + IDs);//"5,3"
                console.log("Vendor is " + IDs[i]);//undefined
                $j('.details'+IDs[i]+ ' .priceBlock').empty();      
                $j('.details'+IDs[i]+ ' .priceBlock').append('<span>'+formatCurrency(basePrice,"$")+'</span>');

            });
        }

(i=0;i@Toby Allen谢谢!您对该链接的权利。此链接仅供参考:

function sendRequest(i) {
            var optionSelectionArray = currentlySelectedAttributes(IDs[i]);
            simpleWithAttrPrice(optionSelectionArray, function(data) {
                //var vendor = IDs[i];
                var basePrice = parseFloat(roundDollar(data));
                //newPriceArray[vendor][colorSelected]=basePrice;
                console.log("Vendor is " + IDs);//"5,3"
                console.log("Vendor is " + IDs[i]);//undefined
                $j('.details'+IDs[i]+ ' .priceBlock').empty();      
                $j('.details'+IDs[i]+ ' .priceBlock').append('<span>'+formatCurrency(basePrice,"$")+'</span>');
            });
        }//end sendRequest

        for(i=0; i<IDs.length; i++)
        {   
            sendRequest(i);
        }
函数发送请求(i){
var OptionSelectionAry=当前选定属性(ID[i]);
simpleWithAttrPrice(选项选择光线、函数(数据){
//var供应商=IDs[i];
var basePrice=parseFloat(整数美元(数据));
//newPriceArray[vendor][colorSelected]=基准价格;
console.log(“供应商为”+IDs);/“5,3”
console.log(“供应商是”+IDs[i]);//未定义
$j('.details'+IDs[i]+'.priceBlock').empty();
$j('.details'+IDs[i]+'.priceBlock').append(''+formatCurrency(basePrice,“$”)+'');
});
}//结束发送请求
对于(i=0;i确定和替代项。这是JavaScriptAppable duplicate中的一个常见问题:。基本上,所有回调都在
for
循环终止后运行,此时
i
处于其终值。当每个回调运行时,回调都会查找作用域链以查找
i
,并且每个回调都会找到相同的
i值
for(i=0; i<IDs.length; i++)
        {   
            var vendor = IDs[i];
            var optionSelectionArray = currentlySelectedAttributes(vendor);
            simpleWithAttrPrice(optionSelectionArray, function(data) {
                var basePrice = parseFloat(roundDollar(data));
                //newPriceArray[vendor][colorSelected]=basePrice;
                console.log("Vendor is " + vendor); //only logs this once.
                $j('.details'+vendor+ ' .priceBlock').empty();//If I take this away, appends both prices to same block      
                $j('.details'+vendor+ ' .priceBlock').append('<span>'+formatCurrency(basePrice,"$")+'</span>');

            });
        }
function sendRequest(i) {
            var optionSelectionArray = currentlySelectedAttributes(IDs[i]);
            simpleWithAttrPrice(optionSelectionArray, function(data) {
                //var vendor = IDs[i];
                var basePrice = parseFloat(roundDollar(data));
                //newPriceArray[vendor][colorSelected]=basePrice;
                console.log("Vendor is " + IDs);//"5,3"
                console.log("Vendor is " + IDs[i]);//undefined
                $j('.details'+IDs[i]+ ' .priceBlock').empty();      
                $j('.details'+IDs[i]+ ' .priceBlock').append('<span>'+formatCurrency(basePrice,"$")+'</span>');
            });
        }//end sendRequest

        for(i=0; i<IDs.length; i++)
        {   
            sendRequest(i);
        }