Javascript 增量超过 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10642671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:18:24  来源:igfitidea点击:

Javascript Increment by more than 1?

javascriptincrement

提问by larin555

Here's 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();
    }

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 everytime I click on the plus icon?

我需要的是将“v”(cart_item.quantity)增加一个以上。在这里,它使用“v++”...但它只增加 1。如何更改它以使其每次单击加号图标时增加 4?

I tried

我试过

v++ +4

But it's not working.

但它不起作用。

Thank you!

谢谢!

回答by helpermethod

Use a compound assignment operator:

使用复合赋值运算符:

v += 4;

回答by We Are All Monica

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

使用variable += value;以增量超过之一:

v += 4;

It works with some other operators too:

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

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

回答by Kvam

To increase v by n: v += n

将 v 增加 n: v += n

回答by c.hill

Try this:

尝试这个:

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

回答by Flavio

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

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