Jquery 为什么不是';不是在正确的号码吗?
我在我的产品页面上有一个数量框,当你增加或减少数量时,一个框会突出显示你得到的折扣,但由于某种原因,当它为4时,它会突出显示3包或更多,即使当数量大于等于3时,我的计算也会突出显示 我的代码:Jquery 为什么不是';不是在正确的号码吗?,jquery,Jquery,我在我的产品页面上有一个数量框,当你增加或减少数量时,一个框会突出显示你得到的折扣,但由于某种原因,当它为4时,它会突出显示3包或更多,即使当数量大于等于3时,我的计算也会突出显示 我的代码: //QUANTITY BUTTONS var upBtn = jQuery('#btn-qty-up'); var downBtn = jQuery('#btn-qty-down'); var currentQty = document.getElementB
//QUANTITY BUTTONS
var upBtn = jQuery('#btn-qty-up');
var downBtn = jQuery('#btn-qty-down');
var currentQty = document.getElementById('qty_extention');
var priceTable = jQuery('#multibuy table');
var qty = currentQty.value;
downBtn.click(function(){
var currentQty = document.getElementById('qty_extention');
var qty = currentQty.value;
//console.log(qty);
//console.log('down');
if(!isNaN( qty ) && qty > 0 ){
currentQty.value--;
//VARIBLES DECLARED
var newPrice = jQuery('#dynamic_pricing').find('h1');
var screwinput = jQuery('select#attribute186').find(":selected").text();
var calPrice;
var QtyPrice;
//IF QUANTITY IS MORE THAN X THEN PRICE IS X
switch(true) {
case (qty <= 2):
QtyPrice = '12.95';
priceTable.find('tr:first-child').css('background', 'none');
break;
case (qty >=3 && qty <= 4):
QtyPrice = '12.30';
priceTable.find('tr:first-child').css('background', '#ccc');
priceTable.find('tr:nth-child(2)').css('background', 'none');
break;
case (qty >=5 && qty <= 9):
QtyPrice = '11.65';
priceTable.find('tr:first-child').css('background', 'none');
priceTable.find('tr:nth-child(2)').css('background', '#ccc');
priceTable.find('tr:nth-child(3)').css('background', 'none');
break;
case (qty >=10):
QtyPrice = '10.95';
priceTable.find('tr:nth-child(2)').css('background', 'none');
priceTable.find('tr:nth-child(3)').css('background', '#ccc');
break;
}
jQuery('#qty').val(currentQty.value);
calPrice = (QtyPrice * currentQty.value);
newPrice.html('£' + calPrice.toFixed(2));
}
return false;
});
upBtn.click(function(){
var currentQty = document.getElementById('qty_extention');
var qty = currentQty.value;
//console.log(qty);
//console.log('up');
if( !isNaN( qty )) {
currentQty.value++;
//VARIBLES DECLARED
var newPrice = jQuery('#dynamic_pricing').find('h1');
var screwinput = jQuery('select#attribute186').find(":selected").text();
var calPrice;
var QtyPrice;
//IF QUANTITY IS MORE THAN X THEN PRICE IS X
switch(true) {
case (qty <= 2):
QtyPrice = '12.95';
priceTable.find('tr:first-child').css('background', 'none');
break;
case (qty >=3 && qty <= 4):
QtyPrice = '12.30';
priceTable.find('tr:first-child').css('background', '#ccc');
priceTable.find('tr:nth-child(2)').css('background', 'none');
break;
case (qty >=5 && qty <= 9):
QtyPrice = '11.65';
priceTable.find('tr:first-child').css('background', 'none');
priceTable.find('tr:nth-child(2)').css('background', '#ccc');
priceTable.find('tr:nth-child(3)').css('background', 'none');
break;
case (qty >=10):
QtyPrice = '10.95';
priceTable.find('tr:nth-child(2)').css('background', 'none');
priceTable.find('tr:nth-child(3)').css('background', '#ccc');
break;
}
jQuery('#qty').val(currentQty.value);
calPrice = (QtyPrice * currentQty.value);
newPrice.html('£' + calPrice.toFixed(2));
}
return false;
});
//数量按钮
var upBtn=jQuery(“#btn数量增加”);
var downBtn=jQuery(“#btn数量减少”);
var currentQty=document.getElementById(“数量扩展”);
var priceTable=jQuery(“#多买表”);
var数量=当前数量值;
downBtn.单击(函数(){
var currentQty=document.getElementById(“数量扩展”);
var数量=当前数量值;
//控制台日志(数量);
//console.log('down');
如果(!isNaN(数量)&数量>0){
当前数量值--;
//声明的变量
var newPrice=jQuery(“#动态#u定价”).find('h1');
var screwinput=jQuery('select#attribute186')。查找(“:selected”).text();
var calPrice;
QtyPrice变种;
//如果数量大于X,则价格为X
开关(真){
案例(数量=3和数量=5和数量=10):
QtyPrice='10.95';
priceTable.find('tr:nth child(2)').css('background','none');
priceTable.find('tr:nth child(3)').css('background','#ccc');
打破
}
jQuery('#qty').val(currentQty.value);
计算价格=(数量价格*当前数量值);
newPrice.html(“£”+calPrice.toFixed(2));
}
返回false;
});
向上单击(函数(){
var currentQty=document.getElementById(“数量扩展”);
var数量=当前数量值;
//控制台日志(数量);
//console.log('up');
如果(!isNaN(数量)){
当前数量值++;
//声明的变量
var newPrice=jQuery(“#动态#u定价”).find('h1');
var screwinput=jQuery('select#attribute186')。查找(“:selected”).text();
var calPrice;
QtyPrice变种;
//如果数量大于X,则价格为X
开关(真){
案例(数量=3和数量=5和数量=10):
QtyPrice='10.95';
priceTable.find('tr:nth child(2)').css('background','none');
priceTable.find('tr:nth child(3)').css('background','#ccc');
打破
}
jQuery('#qty').val(currentQty.value);
计算价格=(数量价格*当前数量值);
newPrice.html(“£”+calPrice.toFixed(2));
}
返回false;
});
这就是它的作用:
我的计算可能有点困难,任何帮助都将不胜感激。现在我们已经看到了您的完整代码,请完全重写我的答案 您正在设置
var qty=currentQty.value
,然后修改值:currentQty.value--代码>
此时,qty
的值不会更改。可以像计算价格时那样直接在交换机中访问currentQty.value
,也可以确保在修改值后定义了qty
我还要指出,您在这里复制了大量代码。重构更新UI以反映新数量的代码可能会首先帮助您避免这个问题
考虑这样做:
downBtn.click(function(){
document.getElementById('qty_extention').value--;
updatePrice();
});
upBtn.click(function(){
document.getElementById('qty_extention').value++;
updatePrice();
});
function updatePrice() {
var qty = document.getElementById('qty_extention').value;
// set both new price and table highlights here according to updated price
}
如果#qty_extention
不是只读的,您还应该在它的更改事件上运行updatePrice
,以防有人在不使用您的按钮的情况下手动输入新数量。现在我们已经看到了您的完整代码,请完全重写我的答案
您正在设置var qty=currentQty.value
,然后修改值:currentQty.value--代码>
此时,qty
的值不会更改。可以像计算价格时那样直接在交换机中访问currentQty.value
,也可以确保在修改值后定义了qty
我还要指出,您在这里复制了大量代码。重构更新UI以反映新数量的代码可能会首先帮助您避免这个问题
考虑这样做:
downBtn.click(function(){
document.getElementById('qty_extention').value--;
updatePrice();
});
upBtn.click(function(){
document.getElementById('qty_extention').value++;
updatePrice();
});
function updatePrice() {
var qty = document.getElementById('qty_extention').value;
// set both new price and table highlights here according to updated price
}
如果#quantity_extension
不是只读的,您还应该在它的change
事件上运行updatePrice
,以防有人在不使用您的按钮的情况下手动输入新数量。看到,对于3
它显示38,95
,这是未贴现的,但显然是三个项目价格的倍数,在执行切换后,但在设置最终价格之前,qty
似乎会更新。开关触发的是哪个事件?因为您只有两个选项,请尝试qty===3 | | qty==4
这些是变量赋值,@Darren,这里可能的原因不是=
会在OP的javascript引擎中被破坏。你在引入错误而没有解决任何问题。@DavidHedlund谢谢-typo-my bad你能再分享一点代码吗?qty变量是如何分配的?案例陈述看起来不错,所以我认为它一定是变量。还有一种方法是使用console.log(qty)在脚本的不同阶段实际获取qty变量的值;为了确保它的值是正确的。对于3
,它显示了38,95
,这是未贴现的,但显然是三个项目价格的倍数,似乎数量
在执行切换后,但在设置最终价格之前更新。开关触发的是哪个事件?因为您只有两个选项,