我无法弄清楚错误的原因。我一直收到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>
答案 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');
}
});
});