weixin_33691700 2014-11-05 13:47 采纳率: 0%
浏览 20

用ajax更新购物车

Hi I've made an eshop and what I want to achieve is when I click the appropriate "add to cart button" I want the top cart (not the main cart-page of the eshop) to be updated. Top cart is one div at the header of the eshop. When I refresh the page the right content appears but this doesn't happen without refresh. I know this must be accomplished with ajax but it seems I am doing something wrong.

Part of my code:

<input type="button" class="add_to_cart" id="add_to_cart" value="BUY" />
<script type="text/javascript">
    $('input.add_to_cart').click(function () {
        $.ajax({ 
            type: "POST",
            url:"add_to_topcart.php",
            data:$('.prod_form').serialize(), 
            success:function(data){
                $('#show-quick-cart').text(data); 
               $(".add_cart_msg").delay(250).fadeIn("slow").delay(2000).fadeOut("slow");
             }
         });
     });
</script>
</form>
</div>

<div class="add_cart_msg" style="display:none;"><img src="images/green_tick.png"/>Product was added to cart!</div>

The above has a result to temporarily count the products I add to cart

  • 写回答

1条回答 默认 最新

  • weixin_33728268 2014-11-05 13:53
    关注

    First of all, enclose your code in a document ready handler -

    $(document).ready(function() {
        $('input.add_to_cart').click(function () {
            $.ajax({ 
                type: "POST",
                url:"add_to_topcart.php",
                data:$('.prod_form').serialize(), 
                success:function(data){
                    $('#show-quick-cart').text(data); 
                    $(".add_cart_msg").delay(250).fadeIn("slow").delay(2000).fadeOut("slow");
                }
            });
         });
    });
    

    Then open your browser's console and any errors will be logged. You can then fix them one at a time or post them here if you need help troubleshooting.

    评论

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错