JavaScript shopping cart development tutorial to implement the minus sign function
We learned in the previous section how to implement the plus sign function. Now let’s explain the minus sign function.
Let’s take a look at the code in the previous section first.
名称 | 单价 | 数量 | 总价 |
---|---|---|---|
手表 | 1999 | - + | 1999 |
Let’s continue writing on this basis. Minus sign function
The function of the minus sign is very simple. We can copy the code for the plus sign and then modify it slightly
In this way, our minus sign function is realized , but it needs to be noted that there is still a little problem. After we reduce it to 1, we can continue to reduce it, so a negative number will appear, so we need to deal with this.
When we When the quantity value is greater than 1, it can be reduced continuously. When it is not greater than 1, we can give the quantity box a default value
The code is as follows:
In this way, we have a shopping cart addition and subtraction function , we have completed the function of changing the total price as the quantity changes. The complete code is as follows:
名称 | 单价 | 数量 | 总价 |
---|---|---|---|
手表 | 1999 | - + | 1999 |