简体   繁体   English

如果购物车中没有商品,则隐藏AJAX购物车商品编号

[英]Hide AJAX Cart item numers if no item in cart

I'm struggling with some javascript to figure out the proper way to make it work. 我正在努力使用一些JavaScript,以找出使其正常工作的正确方法。

I have a button showing the number of items in the cart. 我有一个按钮,显示购物车中的物品数量。 By default is zero. 默认为零。 As items added the cart the number is increasing. 随着物品的增加,购物车的数量也在增加。 But at the beginning, I don't want to show "0" in the cart. 但是起初,我不想在购物车中显示“ 0”。

HTML: HTML:

<p id="cart_button" onclick="show_cart();">
  <input type="button" id="total_items" value="0">
</p>

<div id="mycart"></div>

<div id="item_div">

  <div class="items" id="item1">
    <input type="button" value="Add To CART" onclick="cart('item1')">
    <p>Simple Navy Blue T-Shirt</p>
    <input type="hidden" id="item1_name" value="ITEM-ID1">
  </div>

  <div class="items" id="item2">
    <input type="button" value="Add To CART" onclick="cart('item2')">
    <p>Trendy T-Shirt With Back Design</p>
    <input type="hidden" id="item2_name" value="ITEM-ID2">
  </div>

</div>

JAVASCRIPT: JAVASCRIPT:

$(document).ready(function() {

  $.ajax({
    type: 'post',
    url: 'store_items.php',
    data: {
      total_cart_items: "totalitems"
    },
    success: function(response) {
      document.getElementById("total_items").value = response;
    }
  });
});

function cart(id) {
  var name = document.getElementById(id + "_name").value;

  $.ajax({
    type: 'post',
    url: 'store_items.php',
    data: {
      item_name: name
    },
    success: function(response) {
      document.getElementById("total_items").value = response;
    }
  });
}

function show_cart() {
  $.ajax({
    type: 'post',
    url: 'store_items.php',
    data: {
      showcart: "cart"
    },
    success: function(response) {
      document.getElementById("mycart").innerHTML = response;
      $("#mycart").slideToggle();
    }
  });
}

I basically want the button with 0 to be hidden until it gets a value. 我基本上希望隐藏具有0的按钮,直到获得值为止。 if it goes back to zero I want it to be hidden again. 如果它回到零,我希望它再次被隐藏。 Thank you for the help! 感谢您的帮助!

You can show/hide when update cart: 您可以在更新购物车时显示/隐藏:

// Add this function
function update_cart(value) {
  document.getElementById("total_items").value = response;

  if (value > 0) {
    // Show the cart
    document.getElementById("total_items").style.display = "block";
  } else {
    // Hide the cart
    document.getElementById("total_items").style.display = "none";
  }
}

Then, you need to change your code, when update cart: 然后,当更新购物车时,您需要更改代码:

$.ajax({
  type: 'post',
  url: 'store_items.php',
  data: {
    total_cart_items: "totalitems"
  },
  success: function(response) {
    update_cart(response);
  }
});

You can add 'change' event listener to this button: 您可以将“更改”事件监听器添加到此按钮:

let totalItems = $('#total_items');

totalItems.change(function () {
  if (totalItems.val() == 0) {
    totalItems.hide();
  }
  else totalItems.show();
});

Also you should trigger this event in your success method of ajax: 另外,您还应该在ajax的成功方法中触发此事件:

success: function(response) {
      document.getElementById("total_items").value = response;
      totalItems.change();
    }

And finally hide this button at start: 最后在开始时隐藏此按钮:

<input type="button" id="total_items" value="0" style="display: none">

Check this working in fiddle: https://jsfiddle.net/xpvt214o/771844/ 检查此工作是否在小提琴中进行: https : //jsfiddle.net/xpvt214o/771844/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM