Home > Web Front-end > JS Tutorial > Shopping cart function implemented by JQuery (can reduce or add products and automatically calculate prices)_jquery

Shopping cart function implemented by JQuery (can reduce or add products and automatically calculate prices)_jquery

WBOY
Release: 2016-05-16 16:20:30
Original
1637 people have browsed it

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:

Copy code The code is as follows:





Script Home












Unit price:1.50
        
        
        

Unit price:3.95
        
        
        

Total price:




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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template