执行以下函数时,会记录“数量为 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