18

Here's my script :

function itemQuantityHandler(operation, cart_item) {
  var v = cart_item.quantity;

  //add one
  if (operation === 'add' && v < settings.productBuyLimit) {
    v++;
  }

  //substract one
  if (operation === 'subtract' && v > 1) {
    v--;
  }

  //update quantity in shopping cart
  $('.item-quantity').text(v);

  //save new quantity to cart
  cart_item.quantity = v;
}

What I need is to increase v (cart_item.quantity) by more than one. Here, it's using v++, but it's only increasing by 1. How can I change this to make it increase by 4 every time I click on the plus icon?

I tried

v++ +4

But it's not working.

4

5 回答 5

41

使用复合赋值运算符:

v += 4;
于 2012-05-17T20:03:24.887 回答
20

用于variable += value;增加一个以上:

v += 4;

它也适用于其他一些运算符:

v -= 4;
v *= 4;
v /= 4;
v %= 4;
v <<= 1;
v >>= 4;
于 2012-05-17T20:02:32.623 回答
3

将 v 增加 n:v += n

于 2012-05-17T20:02:04.253 回答
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();
    }
于 2012-05-17T20:01:53.630 回答
-1

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

于 2020-03-15T04:16:05.410 回答