Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. 2 x type1 (100+100) -> 1 x type2
  2. type1 + type3 (100+300) -> 1 x type4
  3. type3 + type4 (300+400) -> 1 x type6 + 1 x type2
  4.  
  5. 1 type6 + 1 type2 + 1 type1 -> 1 x type6 + 1 x type3
  6.  
  7. var types= {
  8. b1: {price:100,quantity:0},
  9. b2: {price:200,quantity:0},
  10. b3: {price:300,quantity:0},
  11. b4: {price:400,quantity:0},
  12. b5: {price:500,quantity:0},
  13. b6: {price:500,quantity:0}
  14. }
  15. function getTotal() {
  16. var total = 0;
  17. $.each(types,function(typeId,type) {
  18. total+=type.quantity*type.price;
  19. });
  20. return total
  21. }
  22. $(function() {
  23. var cont = $("#container");
  24. $.each(types,function(typeId,type) {
  25. $('<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);
  26. });
  27. $(".add").on("click",function() {
  28. var id = this.id.replace("add",""),type=types[id];
  29. type.quantity++;
  30. var subTotal = type.quantity*type.price;
  31. $("#"+id+"val").text(type.quantity);
  32. $("#"+id+"total").text(subTotal);
  33. $("#total").text(getTotal());
  34. });
  35. $(".remove").on("click",function() {
  36. var id = this.id.replace("remove",""),type=types[id];
  37. if (type.quantity>0) type.quantity--;
  38. var subTotal = type.quantity*type.price;
  39. $("#"+id+"val").text(type.quantity);
  40. $("#"+id+"total").text(subTotal);
  41. $("#total").text(getTotal());
  42. });
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement