简体   繁体   English

无法在 javascript 中使用 + 运算符增加价值

[英]unable to increase value using + operator in javascript

I am trying to increment value by 1 whenever the button is clicked.每当单击按钮时,我都会尝试将值增加 1。 I even convert the value to int by using parseInt() by it keep increasing the counter but not the value.我什至通过使用parseInt()将值转换为 int,因为它不断增加计数器而不是值。

when I clicked the button whose value is 1. My expected result will be on click is 2 3 4 5 6 7 and so on...当我单击值为 1 的按钮时。单击时我的预期结果将是 2 3 4 5 6 7 等等...

<div id="move_left_right">
 <button class="btn" value=1 onclick="counter()" id="left"><<</button>
</div>


function counter()
{
    let valueIs  = document.getElementById('left').value;
    let conversion = parseInt(valueIs);
    let sum = conversion + 1;
    localStorage.setItem("getSum" , sum);
}
//getting value of sum outside function using local storage
let x = localStorage.getItem("getSum")
console.log(x);
//setting value to left as 2 
document.getElementById('left').value= x;

//now value is 2 when button is pressed i want the value to be 3 4 5 etc

I edited you function ,now it updates the button's value + it stores in into the local storage :我编辑了你的功能,现在它更新了按钮的值+它存储到本地存储中:

function counter() {
    let btn =  document.getElementById('left')
    let value = parseInt(btn.value);
    let sum = value + 1;
    localStorage.setItem("getSum" , sum);
    btn.value = sum;

    console.log(sum);
    console.log(typeof(sum));
}

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

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