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

标签 javascript jquery ajax

我正在尝试在循环内的函数内正确访问 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>');

            });
        }

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

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?

最佳答案

@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);
        }

关于javascript - 如何在回调函数中正确访问数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032064/

相关文章:

java - 为什么AJAX调用Servlet(异步)会被执行多次?

jquery如何向图像添加图钉并将位置保存到SQL

jquery - 使用 excel-bootstrap-table-filter jQuery 插件时如何对日期进行排序

jquery - jquery中从右到左显示和隐藏div

php - Ajax 发表评论,如 StackOverflow

php - AJAX Jquery PHP 表单验证

javascript - VueJs : How to reuse bootstrap modal dialog

javascript - 如何使用 JQuery 停止然后启动/触发事件?

javascript - 如何引用定义的值

jquery - 在 updatepanel 部分回发中丢失通过 jquery 添加的 css 类