JavaScript development shopping cart tutorial to implement the plus sign function
First let’s look at the following html code
名称 | 单价 | 数量 | 总价 |
---|---|---|---|
手表 | 1999 | - + | 1999 |
Now let’s write the function of the plus sign The id is a2 Look at the code below
First we get the value of the quantity box and get The html content of the total price box, when the plus sign is clicked
Use v1 to receive the value of the quantity box
Note: When the plus sign is clicked, the quantity has changed,
Then we use the parseInt function to convert v1 into a number. The quantity value at this time is already 2
So we use document.getElementById('id').value = v1 + 1;
Then we assign a value to the html of the total price box, and multiply the quantity by the unit price
In this way, when we click the plus sign in the shopping cart, the function of changing the total price has been implemented. The complete code is as follows:
名称 | 单价 | 数量 | 总价 |
---|---|---|---|
手表 | 1999 | - + | 1999 |