Click on the shopping cart to reduce or add items and automatically calculate the price:
The shopping cart may have such a function, that is, clicking a button can reduce or increase the number of goods, and the total product price can be calculated in real time. The following is a code example to introduce how to implement this function. Of course, the following The shopping cart implemented by this simulation is difficult to achieve, but you can get some inspiration or related knowledge points from it. The code is as follows:
The above code implements a simple shopping cart function. Here is a detailed introduction to its implementation process.
Code comments:
1.$(function(){}), when the document structure is completely loaded, execute the code in the function.
2.$(".add").click(function(){}), register the click event handler function for the plus button.
3.var t=$(this).parent().find('input[class*=text_box]'), get the text box, this text displays the number of goods to be purchased.
4.t.val(parseInt(t.val()) 1), click once to increase the quantity of the product by 1.
5.setTotal(), executing this function can calculate the total price and display it.
6.$(".min").click(function(){}), register the click event handler function for the minus button.
7.function setTotal(){}, this function can calculate the total price and display it.
8.var s=0, declare a variable, this variable is used to store the total price.
9.$("#tab td").each(function(){
s =parseInt($(this).find('input[class*=text_box]').val())*parseFloat($(this).find('span[class*=price]').text() );
});
You can iterate over the text boxes and multiply by the unit price, then add them up, and finally calculate the total price.