Ajax Uncaught TypeError:无法读取null的属性“click”

时间:2016-01-30 09:37:36

标签: javascript ajax

我无法弄清楚错误的原因。我一直收到Uncaught TypeError: Cannot read property 'click' of null错误。我确实在按钮中定义了.product-btn类。

以下是完整代码:

<script type="text/javascript">
    $('.product-btn').click(function(){
        if($(this).attr('data-action')=="add"){
          ajaxSubmit('/add/product/'+$(this).attr('data-id')+'/');
              $(this).attr('data-action','remove');
              $(this).children('span').html('Remove from cart');
      }else{
        ajaxSubmit('/remove/product/'+$(this).attr('data-id')+'/');
            $(this).attr('data-action','add');
            $(this).children('span').html('Request Price');
      }
    });
</script>

按钮:

<button class="btn btn-sm btn-inverse btn-embossed product-btn" data-action="add" data-id="<?php echo $productId; ?>"><span><?php echo $this->__('Add to Cart') ?></span></button>

1 个答案:

答案 0 :(得分:0)

准备好功能

$(document).ready(function(){
 $('.product-btn').click(function(){
                if($(this).attr('data-action')=="add"){
                  ajaxSubmit('/add/product/'+$(this).attr('data-id')+'/');
                  $(this).attr('data-action','remove');
                  $(this).children('span').html('Remove from cart');
                }else{
                    ajaxSubmit('/remove/product/'+$(this).attr('data-id')+'/');
                        $(this).attr('data-action','add');
                        $(this).children('span').html('Request Price');
                  }
                });
        });