取消选中框JavaScript后的重置输入值?
这是我的JS代码
let items = 0;
document.addEventListener("click", ({ target }) => {
if (target.className === "food" && target.checked) {
parseInt(items)
items += target.value;
console.log("I clicked the item", items);
} else if (target.className === "food" && !target.checked) {
parseInt(items)
items -= target.value;
console.log("i unclicked", items);
})}
HTML输入,
<input type="checkbox" name="item1" value="1000" class="food">
<input type="checkbox" name="item2" value="2000" class="food">
当我单击一个复选框时,它将添加1000,然后当我取消选中该转换为2000时,然后再次检查它变为3000,并且基本上只是不断添加它们,无论我检查还是取消选中
如何 检查当我取消选中的输入时,我将重置价值?
更新:我添加了ParseInt,并在单击复选框时显示01000,但是当我取消选中为0时。但是,如果我单击1个复选框,那么另一个会像10002000一样添加它,而不是执行3000
Here is my JS code
let items = 0;
document.addEventListener("click", ({ target }) => {
if (target.className === "food" && target.checked) {
parseInt(items)
items += target.value;
console.log("I clicked the item", items);
} else if (target.className === "food" && !target.checked) {
parseInt(items)
items -= target.value;
console.log("i unclicked", items);
})}
HTML Input
<input type="checkbox" name="item1" value="1000" class="food">
<input type="checkbox" name="item2" value="2000" class="food">
Right now when I click a checkbox it will add 1000, then when I uncheck it to turns to 2000, then checking again it becomes 3000, and basically just keeps adding them regardless if I check or uncheck
How do I reset the value when I uncheck my input?
Update: I added parseInt and it shows 01000 when I click my checkbox, but when I uncheck it becomes 0. However, if I click 1 checkbox, then another it will add it like 10002000 instead of doing 3000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者没有parseint:
Alternatively without the parseInt:
我认为这就是您要寻找的。
I think this is what you are looking for.