我正在使用Laravel为电子商务网站并通过这种方式添加项目:
<div class="product-icon-container">
<a href="{{ route('product.addToCart', ['id' => $product->id]) }}"><i class="fa fa-shopping-cart"></i></a>
</div>
你能建议一个AJAX解决方案来添加商品到购物车而不刷新页面吗?
您可以使用Jquery捕获onclick并异步提交。
<script type="text/javascript">
$('.product-icon-container').find('a').click(function (event){
event.preventDefault();
$.ajax({
url: $(this).attr('href')
,success: function(response) {
alert(response)
}
});
return false; //for good measure
});
</script>