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.