【问题标题】:Hide add to cart button when quantity max value is 0 (Shopify)当数量最大值为 0 时隐藏添加到购物车按钮(Shopify)
【发布时间】:2014-04-30 21:40:30
【问题描述】:

我正在尝试使用 Shopify 和 BookThatApp 在线租用产品。

应用程序强制数量字段在预订时具有最大值 0,这意味着您无法继续购买。在这种情况下,出于可用性考虑,我想将“添加到购物车”按钮设为灰色。

自己选择 5 月 13 日和 3 天:

http://propeller-6.myshopify.com/products/007-never-say-never-again-sign

我正在考虑并尝试过类似的事情:

jQuery(document).ready(function($){
    $( "#product-select-option-0" ).change(function() {
      var maxquantity = $('#quantity').attr('max');
      if  (maxquantity = 0) {
         // hide button
      }
    });
});

我接近了吗? :)

谢谢 卡罗琳

【问题讨论】:

  • 不太明白你想要什么...但是如果你真的可以使用javascript访问最大数量,你可以简单地禁用按钮:$('#mybutton').attr('disabled', true)。请注意,您应该在if 条件中使用==
  • 谢谢霍尔特。禁用按钮不是问题,我不确定每次更新下拉列表时是否可以获得最大值。我基本上想在输入最大值为0时隐藏按钮。
  • 哦,感谢您对==的提醒

标签: jquery shopify liquid


【解决方案1】:

好的,主要问题在于change 函数。我让它像这样工作:

$(document).on("change",'#product-select-option-0', function() { 
  var productquantity = $('#quantity').attr('value');
    if  (productquantity == 0) {
        $('#add-to-cart').prop('disabled', true);
      }
})

还将= 更改为==,将.attr('max'); 更改为.attr('value');(因为 max 仅在 Internet Explorer 10、Opera、Chrome 和 Safari 中支持)。

【讨论】:

  • 干得好,尽管您可能仍想考虑使用<= 而不是==,正如我在回答中建议和解释的那样。 :)
【解决方案2】:

我认为它不起作用,因为您没有使用正确的 Javascript 条件。

if (maxquantity = 0) { 不是正确的 Javascript,您可能希望将条件更改为 <= 表示:小于或等于 0 或 == 表示:等于 0。

jQuery(document).ready(function($){
    $( "#product-select-option-0" ).change(function() {
      var maxquantity = $('#quantity').attr('max');
      if  (maxquantity <= 0) {
         // hide button
      }
    });
});

【讨论】:

  • 感谢 Daan,但我意识到我的问题主要在于 .change 函数不起作用。不过我采纳了你的建议并使用了== :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多