简体   繁体   English

如何将输入值添加到计数器值?

[英]how to add input value to counter value?

I'm trying to have a counter that can respond to input values and add it to displayed count but continue to count by 1. The count starts at 0. I have the counter working with buttons, but not with the added input.我正在尝试拥有一个计数器,它可以响应输入值并将其添加到显示的计数中,但继续以 1 计数。计数从 0 开始。我让计数器使用按钮,但不能使用添加的输入。

 let decreaseBtn = document.getElementById("buttonDecrease"); let increaseBtn = document.getElementById("buttonIncrease"); let counter = document.getElementById("counter"); let inputValue = document.getElementById("quantity").inputValue let count = 0; decreaseBtn.addEventListener("click", () => { count--; counter.innerHTML = count; counterStyle(); }); increaseBtn.addEventListener("click", () => { count++; counter.innerHTML = count; counterStyle(); }); function counterStyle() { if (count < 0) { counter.classList.add("negative"); } else if (count > 0) { counter.classList.add("positive"); } else { counter.classList.remove("negative"); counter.classList.remove("positive"); } } function addInput() { console.log(inputValue) } addInput();
 <h1 id="counter">0</h1> <div id="button__wrapper"> <button id="buttonDecrease">-</button> <input type="text" id="quantity" value="1"> <button id="buttonIncrease">+</button> </div>

Yuo need to get the value of the input in your event listener functions, not when the script starts.您需要在事件侦听器函数中获取输入的值,而不是在脚本启动时获取。 Then use that instead of just incrementing and decrementing.然后使用它而不是仅仅增加和减少。

The property to get the value of an input is .value , not .inputValue .获取输入值的属性是.value ,而不是.inputValue

 let decreaseBtn = document.getElementById("buttonDecrease"); let increaseBtn = document.getElementById("buttonIncrease"); let counter = document.getElementById("counter"); let quantity = document.getElementById("quantity"); let count = 0; decreaseBtn.addEventListener("click", () => { count -= addInput(); counter.innerHTML = count; counterStyle(); }); increaseBtn.addEventListener("click", () => { count += addInput(); counter.innerHTML = count; counterStyle(); }); function counterStyle() { if (count < 0) { counter.classList.add("negative"); counter.classList.remove("positive"); } else if (count > 0) { counter.classList.add("positive"); counter.classList.remove("negative"); } else { counter.classList.remove("negative"); counter.classList.remove("positive"); } } function addInput() { return parseInt(quantity.value); }
 .positive { background-color: green; } .negative { background-color: red; }
 <h1 id="counter">0</h1> <div id="button__wrapper"> <button id="buttonDecrease">-</button> <input type="text" id="quantity" value="1"> <button id="buttonIncrease">+</button> </div>

You have increment and decrement operators when you handle the click event.当您处理点击事件时,您有incrementdecrement运算符。 This is why it is always just adding or subtracting by 1, and not by the value of the input.这就是为什么它总是只是加或减 1,而不是输入的值。 You should be adding the value of the input element, after using parseInt() to make it into an add-able integer.在使用parseInt()使其成为可添加整数后,您应该添加输入元素的值。

Your code that I'm changing:我正在更改的代码:

count--;

What I'm changing it to...我要把它改成什么...

count -= parseInt(document.getElementById('quantity').value, 10);

And I'm doing the same for count++ .我正在为count++做同样的事情。

In addition , it seems your counterStyles() function isn't removing classes appropriately, and sometimes you'll have a red background with positive integers.此外,您的counterStyles()函数似乎没有适当地删除类,有时您会看到带有正整数的红色背景。 I fixed this up by adding the needed removeClass() calls here.我通过在此处添加所需的removeClass()调用来解决此问题。

The rest of the code is the same...其余代码相同...

 let decreaseBtn = document.getElementById("buttonDecrease"); let increaseBtn = document.getElementById("buttonIncrease"); let counter = document.getElementById("counter"); let inputValue = document.getElementById("quantity").inputValue let count = 0; decreaseBtn.addEventListener("click", () => { count -= parseInt(document.getElementById('quantity').value, 10); counter.innerHTML = count; counterStyle(); }); increaseBtn.addEventListener("click", () => { count += parseInt(document.getElementById('quantity').value, 10); counter.innerHTML = count; counterStyle(); }); function counterStyle() { if (count < 0) { counter.classList.add("negative"); counter.classList.remove("positive"); } else if (count > 0) { counter.classList.add("positive"); counter.classList.remove("negative"); } else { counter.classList.remove("negative"); counter.classList.remove("positive"); } } function addInput() { console.log(inputValue) } addInput();
 .positive { background-color: green; } .negative { background-color: red; }
 <h1 id="counter">0</h1> <div id="button__wrapper"> <button id="buttonDecrease">-</button> <input type="text" id="quantity" value="1"> <button id="buttonIncrease">+</button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM