2016-10-21 7 views
0

私はAJAXリクエストを使ってカートの内容を示すカートポップアップを持っています。このポップアップの中に、その広告申込情報をカートから削除する「X」リンクがあります。ただし、次のコードを使用すると、Xリンクの通常のリンク動作が引き続き発生します。つまり、更新されたカートページにすぐに移動します。Shopify AJAXリクエスト - e.preventDefaultが機能しない

$('#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()); 
     } 
     }); 
    }); 

私が間違って何をやっている

<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> 

"リンクを削除" のHTML? (私は、JSコードの同じブロックを使用してポップアップを表示させ、そのために動作します)

ありがとう!

+0

あなたのAJAX呼び出しが 'この角度コード – Sravan

+0

'に入れて '{{forloop.index}}取得されているが、 – Sravan

答えて

0

は、アンカータグからhrefを削除し、クリック機能にで使用します。、

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> 

を今、関数内でこれを使用してデータ属性を参照してください。

$('#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()); 
     } 
     }); 
    }); 
+0

@Kevmonがある、と呼ばれるdocument.ready' – Sravan

+0

>?これを試してみました、私はそれました残念ながら何の効果もありません。 – Kevmon

+0

まだリダイレクトされていますか? 、console.log(this.data( "url")) '、何が入って来るのかを確認してください – Sravan

関連する問題