0

执行以下函数时,会记录“数量为 1”和“出现问题”。输入中的数量确实是1,那么为什么else语句在满足第一个if语句qty==1时执行呢?这只发生在 Chrome 中!在 Firefox 中运行良好。

function updatePrices(IDs){
    var qty= parseInt($j("#qtyUpdateBox input").val());
    console.log("The qty is " + qty);//logs correctly

    if (qty==1){
        function sendRequest(i) {
            var optionSelectionArray = currentlySelectedAttributes(IDs[i]);
            simpleWithAttrPrice(optionSelectionArray, function(data) {
                //var vendor = IDs[i];
                var basePrice = parseFloat(roundDollar(data));
                $j('.details'+IDs[i]+ ' .priceBlock').empty();      
                $j('.details'+IDs[i]+ ' .priceBlock').append('<span>'+formatCurrency(basePrice,"$")+'</span>');
                $j('.details'+IDs[i]+ ' .priceBlock').append('<input type="hidden" name="customPrice" value="' + basePrice + '"/>');
            });
        }//end sendRequest

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

    }//end if
    else{
        //ajax call to obtain tier prices for each vendor id
        function sendRequest(i,qty,product_id){
            var vendor = IDs[i]; 
            $j.ajax({
                    type: "POST",
                    url: "/ajax_calls/updatePrices.php",
                    data: { 'vendorID': vendor, 'product_id': product_id}
                    }).done(function(data) {
                        //console.log('The data is ' + data);
                        //CAITLIN below may need to be parsed in the php script
                            var data= JSON.parse(data);

                            var optionSelectionArray = currentlySelectedAttributes(data.vendor_id);
                            simpleWithAttrPrice(optionSelectionArray, function(price) {
                                var basePrice = roundDollar(parseFloat(price));
                                var pricexQty= basePrice * qty;

                                if (qty < data.tier2_range_start){
                                    var totalPrice = basePrice*qty; 
                                }
                                else if (qty >= data.tier2_range_start && qty < data.tier3_range_start){
                                    var discountPercent = data.tier2_discount;
                                    var discount = pricexQty * data.tier2_discount / 100;
                                    var totalPrice = pricexQty - discount;
                                    var unitPrice = totalPrice/qty;
                                }
                                else if (qty >= data.tier3_range_start && qty < data.tier4_range_start){
                                    var discountPercent = data.tier3_discount;
                                    var discount = pricexQty * data.tier3_discount / 100;
                                    var totalPrice = pricexQty - discount;
                                    var unitPrice = totalPrice/qty;
                                }
                                else if (qty >= data.tier4_range_start && qty < data.tier5_range_start){
                                    var discountPercent = data.tier4_discount;
                                    var discount = pricexQty * data.tier4_discount / 100;
                                    var totalPrice = pricexQty - discount;
                                    var unitPrice = totalPrice/qty;
                                }
                                else if (qty >= data.tier5_range_start){
                                    var discountPercent = data.tier5_discount;
                                    var discount = pricexQty * data.tier5_discount / 100;
                                    var totalPrice = pricexQty - discount;
                                    var unitPrice = totalPrice/qty; 
                                }
                                else{
                                    console.log('Something went wrong');
                                }

                                unitPrice = roundDollar(unitPrice);


                                $j('.details'+data.vendor_id+ ' .priceBlock').empty();//update product price in DOM
                                if (discountPercent)
                                    $j('.details'+data.vendor_id+ ' .priceBlock').append('<h5 style="color:gold">You will save '+discountPercent+'% !</h5>');
                                $j('.details'+data.vendor_id+ ' .priceBlock').append('<span>Unit Price: '+formatCurrency(unitPrice,"$")+'</span> / ');
                                $j('.details'+data.vendor_id+ ' .priceBlock').append('<span>Total Price: '+formatCurrency(unitPrice*qty,"$")+'</span>');
                                $j('.details'+data.vendor_id+ ' .priceBlock').append('<input type="hidden" name="customPrice" value="' + unitPrice + '"/>');
                        });//end callback function

                    });//end done function
                }//end function sendRequest

        for(i=0; i<IDs.length; i++)
        {   
            sendRequest(i,qty,product_id);
        }
    }//end else


}//end function 
4

2 回答 2

2

我认为这与方法提升有关。无论 if 语句如何,它都会导致第二个声明覆盖第一个声明。

您可以尝试的解决方法是将方法声明样式更改为var sendRequest = function(){...}fromfunction sendRequest() {...}

可以使用此演示演示该问题

这里展示了一种可能的解决方案

注意:对于发生这种情况的原因,请阅读有关在 javascript 中提升的信息

于 2013-06-11T03:50:48.120 回答
0

更改function sendRequest(...) { body }var sendRequest = function(...) { body }

更好的是,不要在thenandelse子句中使用相同的函数名。

于 2013-06-11T03:51:07.197 回答