Javascript增量超过1?
这是我的剧本:Javascript增量超过1?,javascript,increment,Javascript,Increment,这是我的剧本: //event handler for item quantity in shopping cart function itemQuantityHandler(p, a) { //get current quantity from cart var filter = /(\w+)::(\w+)/.exec(p.id); var cart_item = cart[filter[1]][filter[2]]; v
//event handler for item quantity in shopping cart
function itemQuantityHandler(p, a) {
//get current quantity from cart
var filter = /(\w+)::(\w+)/.exec(p.id);
var cart_item = cart[filter[1]][filter[2]];
var v = cart_item.quantity;
//add one
if (a.indexOf('add') != -1) {
if(v < settings.productBuyLimit) v++;
}
//substract one
if (a.indexOf('subtract') != -1) {
if (v > 1) v--;
}
//update quantity in shopping cart
$(p).find('.item-quantity').text(v);
//save new quantity to cart
cart_item.quantity = v;
//update price for item
$(p).find('.item-price').text((cart_item.price*v).toFixed(settings.numberPrecision));
//update total counters
countCartTotal();
}
但它不起作用
谢谢大家! 试试这个:
//event handler for item quantity in shopping cart
function itemQuantityHandler(p, a) {
//get current quantity from cart
var filter = /(\w+)::(\w+)/.exec(p.id);
var cart_item = cart[filter[1]][filter[2]];
var v = cart_item.quantity;
//add four
if (a.indexOf('add') != -1) {
if(v < settings.productBuyLimit) v += 4;
}
//substract one
if (a.indexOf('subtract') != -1) {
if (v > 1) v--;
}
//update quantity in shopping cart
$(p).find('.item-quantity').text(v);
//save new quantity to cart
cart_item.quantity = v;
//update price for item
$(p).find('.item-price').text((cart_item.price*v).toFixed(settings.numberPrecision));
//update total counters
countCartTotal();
}
//购物车中商品数量的事件处理程序
函数itemQuantityHandler(p,a){
//从购物车获取当前数量
变量过滤器=/(\w+):(\w+/.exec(p.id);
var cart_item=cart[filter[1]][filter[2]];
var v=购物车\商品数量;
//加四
如果(a.indexOf('add')!=-1){
如果(v1)v--;
}
//更新购物车中的数量
$(p).find('.item quantity').text(v);
//将新数量保存到购物车
cart_item.quantity=v;
//更新项目的价格
$(p).find('.item price').text((cart\u item.price*v).toFixed(settings.numberPrecision));
//更新总计数器
countcartotal();
}
将v增加n:
v+=n使用
变量+=value代码>增加一个以上:
v += 4;
它还与其他一些运营商合作:
v -= 4;
v *= 4;
v /= 4;
v %= 4;
v <<= 1;
v >>= 4;
v-=4;
v*=4;
v/=4;
v%=4;
v=4;
使用复合赋值运算符:
v += 4;
var i=0;
函数按钮单击(){
x=++i*10+10;
}
v+=4
。。。?还有一些填充…如果你不知道如何给一个数字加上4,我真的建议你学习一个基本的编程教程。不等效:v+=4
只对表达式求值一次,当它是一个长表达式或有副作用时,这是很好的。v+=1的作用类似于++v,而不是v++作为参考:请参见(+=
)和(-=
)运算符。| |=和&&=在JavaScript中似乎不存在。|=和&=执行按位赋值运算符,如果你不直接比较实际的布尔值,它们同样有效。你是对的@Csit-我写这篇文章时一定用了另一种语言思考。
v += 4;