jquery implementation Imitation Taobao shopping cart settlement two examples

Source: Internet
Author: User


In this article, all the features in the shopping cart will be mentioned. Includes all selections, change in the amount of a single election. The amount will change accordingly when the quantity is increased.


The general idea:

1, the first is to calculate the price of a row. This feature is mentioned in the previous blog, this is not listed here.
2, traversing the selected lines, add the values of each row.
3, the value assigned to the total amount displayed. When you cancel the check or decrease the quantity, the amount changes accordingly.

<script type= "Text/javascript" >
$ (function () {
Calculate Total Amount
function Totalmoney () {
var total_price = 0;
var total_count = 0;
$ (". D1-input"). each (function () {
if ($ (this). Hasclass (' status ')) {
var obj1 = $ (this). Parent (). Siblings (". d4"). Find (". Count"). Val ();//Get the number of items in a row
Total_count + + parseint (obj1);
var obj2= $ (this). Parent (). Siblings (". d5"). Find (". Cart-price"). Text ();//Get the amount of one line
Total_price + + parseint (OBJ2);
}
});
$ (". Item"). Text (total_count);//To display the total amount calculated
$ (". Total"). Text (total_price);//To display the calculated quantity
}
Select All
$ (". D-input"). Toggle (function () {
$ (this). addclass ("status");
$ (". Sc-mid-list. D1-input"). AddClass ("status");//Add Check Style
$ (". Sc-mid-list"). AddClass ("MouseOver");
Totalmoney ();
},
function () {
$ (this). Removeclass ("status");
$ (". Sc-mid-list. D1-input"). Removeclass ("status");//Cancel Check style
$ (". Sc-mid-list"). Removeclass ("MouseOver");
Totalmoney ();
});
Radio
$ (". D1-input"). Toggle (function () {
$ (this). addclass ("status");//Add Check Style
$ (this). Parents (". Sc-mid-list"). AddClass ("MouseOver");
Totalmoney ();
},
function () {
$ (this). Removeclass ("status");/cancel Check style
$ (this). Parents (". Sc-mid-list"). Removeclass ("MouseOver");
Totalmoney ();
});
Minus style
function count () {
$ (". Count"). each (function () {
var num = $ (this). Val ();
if (num = 1) {
$ (this). Siblings (". reduce"). Removeclass ("two"). AddClass ("one");
$ (this). Siblings (". reduce"). attr ("Disabled", "disabled");
} else {
$ (this). Siblings (". reduce"). Removeclass ("one"). AddClass ("two");
}
})//change the style of the minus sign according to whether the quantity is 1
}
Add
$ (". Sc-mid-list. Add"). Click (function () {
var input = $ (this). Siblings (". Count");
var obj = $ (this). Parents (". d4");
var per = parsefloat (Obj.siblings ("./d3"). Find (". per"). text (); Get the unit price of the current line
var num = ';
var price = ';
Input.attr ("Value", parseint (input.attr ("value")) + 1);//Quantity plus 1
num = input.attr ("value");
Price = num*per;//
Obj.siblings (". d5"). Find (". Cart-price"). Text (price);
Count ();
Totalmoney ();
})
Reducing
$ (". Sc-mid-list. Reduce"). Click (function () {
var input = $ (this). Siblings (". Count");
var obj = $ (this). Parents (". d4");
var per = parsefloat (Obj.siblings ("./d3"). Find (". per"). text (); Get the unit price of the current line
var num = ';
var price = ';
var Val = parseint (input.attr ("value"));
if (Val <= 1) {
Val = 2;
}
Input.attr ("Value", parseint (Val)-1);//Quantity minus 1
num = input.attr ("value");
Price = num*per;//
Obj.siblings (". d5"). Find (". Cart-price"). Text (price);
Count ();
})
})
</script>

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.