javascript - 为什么我的购物车增量计数器不起作用?
问题描述
每次单击“添加到购物车”按钮时,我希望导航栏中的计数增加一个项目,但是当我当前单击“添加到购物车”按钮时,我看不出为什么我的代码不起作用不增加。如果有人能告诉我我缺少什么或给我一个更好的解决方案,我将不胜感激,谢谢!
$('.addCart').on('click', function() {
console.log(completedIncrements.indexOf(this.id));
if (completedIncrements.indexOf(this.id) == -1) {
var count = parseInt($("#count").text());
$("#count").html(count + 1);
completedIncrements.push(this.id);
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="navbar">
<a href="shoppingcart.html" class="cart">Shopping Cart (<span id="count">0</span>)</a>
<a href="shop.html" class="right">Shop</a>
<a href="contactus.html" class="right">Contact Us</a>
</div>
<a href="shoe1.html"><img id="shoe3" img src="images/shoe1.jpg" alt="shoe1">
<div id="cart1" class="addCart" href="#"><button>Add to cart</button></div>
<a href="shoe2.html"><img id="shoe4" img src="images/shoe2.jpg" alt="shoe2">
<div id="cart2" class="addCart" href="#"><button>Add to cart</button></div>
解决方案
我刚刚检查了你的代码。completeIncrements 在控制台中引发错误并阻止执行您的逻辑。请提供有关该问题的信息,或者使用下面的代码来解决它。
$('.addCart').on('click', function() {
var count = parseInt($("#count").text());
$("#count").html(count + 1);
})
推荐阅读
- python - Pytest跳过参数化
- javascript - 如何使用 ajax 在 Asp.net 中填充下拉列表
- apache - 在不同的 GCP 项目上运行 Apache 和 Nginx 并使用相同的域
- google-sheets - 谷歌表格:部分匹配的 vLookup
- android - 我的应用在使用BCR识别时意外退出,报错“NoSuchMethodError”
- flutter - 将 ZIP 文件发送到基于 REST 的 API,该 API 使用基于 Flutter 的移动应用程序托管在 AWS 上的 SSL TLS (https)
- java - SWT ShellEvent 源属性
- r - 避免矩形覆盖轴
- javascript - 在以下 Vue.js 方法中,对象数组没有被过滤,过滤项目中的值也没有被改变
- java - 如何为 java netbeans 应用程序设置在线数据库