Increase Javascript by more than 1?

Here is my script:

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

I need to increase the "v" (cart_item.quantity) by more than one. Here it uses "v ++" ... but it only increases by 1. How can I change this to increase it by 4 every time I click the plus sign?

I tried

 v++ +4 

But it does not work.

Thanks!

+6
source share
4 answers

Use the complex assignment operator:

 v += 4; 
+15
source

Use variable += value; to increase by more than one:

 v += 4; 

It also works with some other operators:

 v -= 4; v *= 4; v /= 4; v %= 4; v <<= 1; v >>= 4; 
+14
source

To increase v by n: v + = n

+3
source

Try the following:

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

Source: https://habr.com/ru/post/916031/


All Articles