我想初始化一个全局变量,然后在函数中使用它来计算点击次数,然后在for循环内的另一个函数中使用计算出的输出来设置最大限制,即for(i=0; i >= count; i++)
。我在这里遇到的问题是,由于变量是全局变量,因此无法在我的SubmitForm()函数中使用onClick函数的计算值。变量'count'的值被重新分配为0,因为它是全局的。
let count=0;
var add=document.querySelector('.fa-plus');
add.addEventListener('click', () =>{
//Product Label
count=count+1;
}
//to check if the variable has the computed result
console.log(count);
function submitForm(e){
for(i=0; i <= count; i++){
const prod[i] = document.querySelector('#prod-'+i).value;
ipcRenderer.send('item:add', item);
}
}
答案 0 :(得分:-1)
const
您最好还是使用var而不是let,因为尝试更改let通常会导致语法错误。
基于https://www.w3schools.com/jsref/met_element_addeventlistener.asp
(此外,除非您希望for循环使count + 1循环,否则将其设为i