执行以下功能时,会记录“数量为1”和“出错”。输入中的数量确实为1,那么为什么在执行第一个if语句qty == 1时执行else语句?这只发生在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
答案 0 :(得分:2)
我认为这与method hoisting有关。它会导致第二个声明覆盖第一个声明而不管if语句。
您可以尝试修复的方法是将方法声明样式从var sendRequest = function(){...}
更改为function sendRequest() {...}
一种可能的解决方案是demonstrated here
注意:为什么会发生这种情况,请阅读在javascript中提升
答案 1 :(得分:0)
将function sendRequest(...) { body }
更改为var sendRequest = function(...) { body }
。
更好的是,不要在then
和else
子句中使用相同的函数名称。