gpt4 book ai didi

javascript - Chrome 中执行错误的 if 语句

转载 作者:行者123 更新时间:2023-12-02 18:38:47 25 4
gpt4 key购买 nike

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

最佳答案

我认为这与method hoisting有关。它会导致第二个声明覆盖第一个声明,而不管 if 语句如何。

您可以尝试的修复方法是将方法声明样式更改为 var sendRequest = function(){...}来自function sendRequest() {...}

可以使用 this demo 来演示该问题

一种可能的解决方案是 demonstrated here

注意:要了解发生这种情况的原因,请阅读javascript 中的提升

关于javascript - Chrome 中执行错误的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17036168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com