The shopping cart feature implemented by jquery (can reduce or add goods and automatically calculate prices) _jquery

Source: Internet
Author: User

Shopping cart clicks can reduce or add items and automatically calculate prices:

Shopping cart may have such a function, that is, click on the button can achieve a reduction or increase in the number of goods, and can be real-time calculation of the total price of goods, the following is a code example to introduce how to achieve this function, of course, the following simulation of the implementation of the shopping cart is difficult But you can get some inspiration or relevant knowledge points, the code is as follows:

Copy Code code as follows:

<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Cloud-dwelling community </title>
<script type= "Text/javascript" src= "Mytest/jquery/jquery-1.8.3.js" ></script>
<script type= "Text/javascript" >
$ (function () {
$ (". Add"). Click (function () {
var t=$ (this). Parent (). Find (' input[class*=text_box] ');
T.val (parseint (T.val ()) +1)
Settotal ();
})
$ (". Min"). Click (function () {
var t=$ (this). Parent (). Find (' input[class*=text_box] ');
T.val (parseint (T.val ())-1)
if (parseint (T.val ()) <0) {
T.val (0);
}
Settotal ();
})
function Settotal () {
var s=0;
$ ("#tab TD"). each (function () {
S+=parseint ($ (this). Find (' input[class*=text_box] '). Val ())
*parsefloat ($ (this). Find (' span[class*=price] '). text ());
});
$ ("#total"). HTML (s.tofixed (2));
}
Settotal ();
})
</script>
<body>
<table id= "tab" >
<tr>
<td>
<span> asp: </span><span class= "Price" >1.50</span>
<input class= "min" name= "type=" button "value="-"/>"
<input class= "Text_box" name= "type=" text "value=" 1 "/>
<input class= "Add" name= "" type= "button" value= "+"/></td>
</tr>
<tr>
<td>
<span> asp: </span><span class= "Price" >3.95</span>
<input class= "min" name= "type=" button "value="-"/>"
<input class= "Text_box" name= "type=" text "value=" 1 "/>
<input class= "Add" name= "" type= "button" value= "+"/></td>
</tr>
</table>
<p> Price: <label id= "Total" ></label></p>
</body>

The above code implements the simple shopping cart function, the following details its implementation process.

Code comments:

1.$ (function () {}) to execute code in the function when the document structure is fully loaded.
2.$ (". Add"). Click (function () {}) to register the click event handler for the plus button.
3.var t=$ (this). Parent (). Find (' input[class*=text_box] ') to get the text box, which shows the number of items to buy.
4.t.val (parseint (T.val ()) +1), click on the number of items plus 1.
5.setTotal (), perform this function to calculate the total price and display.
6.$ (". Min"). Click (function () {}) to register the click event handler for the minus button.
7.function settotal () {}, this function can calculate the total price and display it.
8.var s=0, declaring a variable that is used to store the total price.
9.$ ("#tab TD"). each (the function () {
  S+=parseint ($ (this). Find (' input[class*=text_box] '). Val ()) * Parsefloat ($ (this). Find (' span[class*=price] '). text ());
}); The
can traverse the text box and multiply it by the unit price, and then sum it up, finally calculating the total price.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.