Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2 x type1 (100+100) -> 1 x type2
- type1 + type3 (100+300) -> 1 x type4
- type3 + type4 (300+400) -> 1 x type6 + 1 x type2
- 1 type6 + 1 type2 + 1 type1 -> 1 x type6 + 1 x type3
- var types= {
- b1: {price:100,quantity:0},
- b2: {price:200,quantity:0},
- b3: {price:300,quantity:0},
- b4: {price:400,quantity:0},
- b5: {price:500,quantity:0},
- b6: {price:500,quantity:0}
- }
- function getTotal() {
- var total = 0;
- $.each(types,function(typeId,type) {
- total+=type.quantity*type.price;
- });
- return total
- }
- $(function() {
- var cont = $("#container");
- $.each(types,function(typeId,type) {
- $('<button class="add" id="'+typeId+'add">Add to '+typeId+'<button><span id="'+typeId+'val">0</button><button class="remove" id="'+typeId+'remove">Remove from '+typeId+'</button><span id="'+typeId+'total">0</span><br/>').appendTo(cont);
- });
- $(".add").on("click",function() {
- var id = this.id.replace("add",""),type=types[id];
- type.quantity++;
- var subTotal = type.quantity*type.price;
- $("#"+id+"val").text(type.quantity);
- $("#"+id+"total").text(subTotal);
- $("#total").text(getTotal());
- });
- $(".remove").on("click",function() {
- var id = this.id.replace("remove",""),type=types[id];
- if (type.quantity>0) type.quantity--;
- var subTotal = type.quantity*type.price;
- $("#"+id+"val").text(type.quantity);
- $("#"+id+"total").text(subTotal);
- $("#total").text(getTotal());
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement