【发布时间】:2016-05-08 02:41:05
【问题描述】:
我无法找出错误的原因。我不断收到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>
【问题讨论】:
-
你包含了 jQuery 吗?
console.log($.fn.jquery)的输出是什么? -
是的,我已经包含了 jQuery。我得到
Uncaught TypeError: Cannot read property 'jQuery' of undefined和console.log($.fn.jquery) -
那么
$.fn不应该是undefined再次检查包含 -
jquery 应该包含在你的脚本之前(最好包含在 head 部分中),并且还应该在关闭 body 标记之前包含你的脚本。
-
@Andreas 我仍然收到
Uncaught TypeError: Cannot read property 'jquery' of undefined这可能是因为prototype.js因为我也包含了它吗? @viktor-kukurba 是的,jQuery位于标题的首位。
标签: javascript ajax