compare.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //加载对比商品
  2. function loadCompare(isrefresh) {
  3. $("#comparelist").load(HOMESITEURL + '/Compare/showcompare.html');
  4. return;
  5. if (!$("#comparelist").html()) {
  6. isrefresh = true;
  7. }
  8. if (isrefresh == true) {
  9. $("#comparelist").load(HOMESITEURL + '/Compare/showcompare.html');
  10. }
  11. }
  12. //添加对比商品
  13. function addCompare(gid) {
  14. gid = parseInt(gid);
  15. if (gid > 0) {
  16. $.ajax({
  17. type: "GET",
  18. dataType: "json",
  19. url: HOMESITEURL + '/Compare/addcompare.html?id=' + gid,
  20. async: false,
  21. success: function (data) {
  22. if (data.done == true) {
  23. $("[ds_type='compare_" + gid + "']").addClass('selected');
  24. loadCompare(true);
  25. $(".top #content-compare").animate({right: '40px'});
  26. } else {
  27. layer.alert(data.msg);
  28. }
  29. }
  30. });
  31. } else {
  32. layer.alert('参数错误');
  33. }
  34. $("#lockcompare").val('unlock');//解除加入对比按钮的锁定
  35. }
  36. //清空对比栏
  37. function delCompare(gid, type) {
  38. $.ajax({
  39. type: "GET",
  40. dataType: "json",
  41. url: HOMESITEURL + '/Compare/delcompare?gid=' + gid + '&type=' + type,
  42. async: false,
  43. success: function (data) {
  44. if (data.done == true) {
  45. //将对比按钮置为未对比状态
  46. if (type == 'mini') {
  47. if (gid == 'all') {
  48. $("[ds_type^='compare_']").removeClass('selected');
  49. } else {
  50. $("[ds_type='compare_" + gid + "']").removeClass('selected');
  51. }
  52. }
  53. //加载对比信息
  54. if (type == 'mini') {
  55. //加载对比栏
  56. loadCompare(true);
  57. //$("#content-compare").animate({right: '40px'});
  58. } else {
  59. go(HOMESITEURL + '/Compare/index/' + (data.gid_str?('gids/'+data.gid_str):''));
  60. }
  61. }
  62. $("#lockcompare").val('unlock');//解除加入对比按钮的锁定
  63. }
  64. });
  65. }
  66. //初始加入对比按钮
  67. function initCompare() {
  68. //绑定对比按钮事件
  69. $("[ds_type^='compare_']").bind('click', function () {
  70. if ($("#lockcompare").val() == 'unlock') {
  71. $("#lockcompare").val('lock');//锁定加入对比按钮
  72. //处理参数
  73. var data_str = '';
  74. eval('data_str =' + $(this).attr('data-param'));
  75. var gid = data_str.gid;
  76. if ($(this).hasClass('selected')) {
  77. $(this).removeClass('selected');
  78. //删除该对比商品
  79. delCompare(gid, 'mini');
  80. } else {
  81. //新增该对比商品
  82. addCompare(gid);
  83. }
  84. }
  85. });
  86. //根据是否已加入对比,显示不同样式
  87. $.getJSON(HOMESITEURL + '/Compare/checkcompare', function (data) {
  88. if (data) {
  89. $.each(data, function (i, val) {
  90. $("[ds_type='compare_" + val + "']").addClass('selected');
  91. });
  92. }
  93. });
  94. }