2017-02-23 42 views
1

這裏的景色如何在鏈接之前重新加載當前頁面? AJAX的jQuery

<a href="{{$cart_items->contains('id',$productItem->id) ? route('IndexCart'): route('AddToCart')}}" class="item_add" id="{{$productItem->id}}"><p class="number item_price {{$cart_items->contains('id',$productItem->id) ? 'added': ''}}"> 
       <i> </i>${{$productItem->price}} 
          </p> 
         </a> 

所以我需要它

第2點擊後重新加載當前頁面嘗試這樣的:

$(document).ready(function(c) { 
     $('.item_add').click(function (event) { 
     var addButton = $(this).children('p'); 
    if(addButton.hasClass('added')) 
    { 
     linktocart.attr('onclick','location.reload(true)'); 
    } 

    else 
    { 
     event.preventDefault(); 
     var id = $(this).attr('id'); 
     var href = $(this).attr('href'); 
     var data = {'cart_item_id': id}; 
     var carturl = "http://site.loc/cart"; 
      $.ajax({ 
       url: href, 
       data: data, 
       headers: { 
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
       }, 
       type: 'POST', 
       dataType: 'JSON', 
       success: function (html) { 
         addButton.addClass('added'); 
         addButton.css('background', 'green'); 
         addButton.parent('a').attr('href', carturl); 
         $('#addOrAlready').text('Added'); 
       } 
      }); 
    } 
}); 

它不工作,我猜,因爲當我第一次點擊它時,我阻止了默認設置,所以我認爲我需要「un」阻止默認設置在連接工作之前進行重新加載。我該怎麼做?我需要它,因爲我猜chrome加載頁面的緩存副本,它沒有在服務器上應用更改。

+1

嘗試在您的問題中正確縮進代碼 –

回答

0

嘗試處理mousedown事件而不是點擊。

相關問題