简体   繁体   English

Shopify AJAX请求-e.preventDefault不起作用

[英]Shopify AJAX Request - e.preventDefault Not Working

I have a cart popup that shows the contents of the cart using an AJAX request. 我有一个购物车弹出窗口,它使用AJAX请求显示购物车的内容。 Inside this popup, I have an "X" link meant to remove that line item from the cart. 在此弹出窗口中,我有一个“ X”链接,用于从购物车中删除该订单项。 However, when I use the following code, 但是,当我使用以下代码时,

$('#remove-from-cart').click(function(e) {  
      var link = $(this).attr('href');
      // Preven link normal behavior
      e.preventDefault();   
      $.ajax({
        url: link,
        type:'GET',
        success: function(data){
          $('#receipt-wrapper .receipt-row-2').html($(data).find('.line-item-container').html());
        }
      });
  });

the normal link behavior of the X link still happens, meaning it takes you to the updated cart page immediately. X链接的正常链接行为仍然会发生,这意味着它会立即将您带到更新的购物车页面。

HTML of "remove link" “删除链接”的HTML

<div class="grid__item receipt--hide small--one-sixth medium--one-sixth large--one-twelfth xlarge--one-twelfth icon-remove">
    <p class="cart__product-meta">
         <a href="/cart/change?line={{ forloop.index }}&amp;quantity=0" id="remove-from-cart">
            {% include 'svg-icon-remove' %}
         </a>
    </p>
</div>

What am I doing wrong? 我究竟做错了什么? (I'm using an identical block of JS code to make the popup appear and it works for that) (我使用的是相同的JS代码块,以使弹出窗口出现,并且它可以正常工作)

Thanks! 谢谢!

Remove href from anchor tag and use in in the click function., 从定位标记中删除href ,并在click功能中使用。,

Take a data attribute and add the required url in the data attribute.

<div class="grid__item receipt--hide small--one-sixth medium--one-sixth large--one-twelfth xlarge--one-twelfth icon-remove">
    <p class="cart__product-meta">
         <a id="remove-from-cart" data-url="/cart/change?line={{ forloop.index }}&amp;quantity=0">
            {% include 'svg-icon-remove' %}
         </a>
    </p>
</div>

Now, refer the data attribute using this in the function. 现在,在函数中使用它来引用数据属性。

$('#remove-from-cart').click(function(e) {  
      var link = this.data("url")
      // Preven link normal behavior
      e.preventDefault();   
      $.ajax({
        url: link,
        type:'GET',
        success: function(data){
          $('#receipt-wrapper .receipt-row-2').html($(data).find('.line-item-container').html());
        }
      });
  });

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

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