Javascript增量超过1?

时间:2012-05-17 20:00:33

标签: 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]];
        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();
    }

我需要的是增加&#34; v&#34; (cart_item.quantity)不止一个。在这里,它使用了&#34; v ++&#34; ...但它只增加1.如何在每次点击加号时将其改为4图标?

我试过

v++ +4

但它没有用。

谢谢!

5 个答案:

答案 0 :(得分:20)

使用复合赋值运算符:

v += 4;

答案 1 :(得分:15)

使用variable += value;增加多个:

v += 4;

它也适用于其他一些运营商:

v -= 4;
v *= 4;
v /= 4;
v %= 4;
v <<= 1;
v >>= 4;

答案 2 :(得分:3)

将v增加n:     v + = n

答案 3 :(得分:0)

试试这个:

//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();
    }

答案 4 :(得分:-1)

var i = 0; function buttonClick() { x = ++i*10 +10; }