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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64