Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript window.onload将每个值设置为0_Javascript - Fatal编程技术网

Javascript window.onload将每个值设置为0

Javascript window.onload将每个值设置为0,javascript,Javascript,不会将所有输入设置为0 onload。我还将它们设置为整数,以便以后添加。您必须设置输入值。==只是将一个值与另一个值进行比较。您需要使用=来设置它们。输入将始终具有字符串值作为输入,因此每次读取值时都必须使用parseIntinput.value,10 window.onload==>{ const form=document.querySelector'input-calculator'; 大堆 .fromform.elements .forEachinput=>input.value='0

不会将所有输入设置为0 onload。我还将它们设置为整数,以便以后添加。

您必须设置输入值。==只是将一个值与另一个值进行比较。您需要使用=来设置它们。输入将始终具有字符串值作为输入,因此每次读取值时都必须使用parseIntinput.value,10

window.onload==>{ const form=document.querySelector'input-calculator'; 大堆 .fromform.elements .forEachinput=>input.value='0'; }
document.querySelector将选择第一个匹配元素;如果要选择多个元素,请使用querySelectorAll。好的做法是不要一次在DOM中有一个Id的多个实例。相反,我们可以使用类。和“querySelectorAll”来获取所有实例。
window.onload = function(){
    let form = document.querySelector('#input-calculator');
    let inputs = Array.from(form.elements)
    let inputValues = inputs.map(e => e.value)
    console.log(inputValues)
    inputValues.forEach(function(item){
    var integer = parseInt(item, 10)
    integer == 0
    });
}