4

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

此方法将 ID 记录为我认为的字符串。我尝试使用索引访问它,但结果未定义。请参阅 simpleWithAttrPrice 函数调用中的 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>');

            });
        }

我还尝试将 ID 传递给回调函数,但它记录“成功”(字面意思)

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

            });
        }

最后,我还尝试了以下方法,但它将价格附加到同一块。

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>');

            });
        }

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

4

1 回答 1

0

@托比艾伦谢谢!您对该链接的权利。作为参考,这个作品:

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);
        }
于 2013-06-10T21:06:09.090 回答