如何在Javascript中获取用户的多个输入?如何为所有人声明变量?

如何在Javascript中获取用户的多个输入?如何为所有人声明变量?,javascript,Javascript,我正在做JavaScript膳食营养计算器。这是一个有5行的模板,每行有4个输入,总共20个,type=value。每个输入都有不同的id 有没有办法在不重复“var x=document.getElementById('x').value”20次的情况下同时读取所有输入 最好的 计算器 成分: 去 事实 结果显示在这里。 函数calo(){ 常量[x,y,z,a]=[…document.queryselectoral('input'); document.querySelector(“pre

我正在做JavaScript膳食营养计算器。这是一个有5行的模板,每行有4个输入,总共20个,type=value。每个输入都有不同的id

有没有办法在不重复“var x=document.getElementById('x').value”20次的情况下同时读取所有输入

最好的


计算器
成分:
去
事实
结果显示在这里。
函数calo(){
常量[x,y,z,a]=[…document.queryselectoral('input');
document.querySelector(“pre”).textContent=x.outerHTML+“\n”+y.outerHTML+“\n”+z.outerHTML+“\n”+a.outerHTML;
var计算=x+y+z+a;
document.getElementById(“结果”).innerHTML=calculate;}
您可以使用

如果希望将
输入
元素声明为单独的变量,可以使用。例如:

const[x,y,z]=[…document.queryselectoral('input');
document.querySelector(“pre”).textContent
=x.outerHTML+“\n”+y.outerHTML+“\n”+z.outerHTML


对于3个输入,“type=name”的外观:document.querySelectorAll('input').forEach(input=>console.log(“name:”,input.id,“Value:”,input.Value));我试过了,但是x,y和z不是declared@Pawel你说不申报是什么意思
document.querySelectorAll
仅获取所有可循环使用的
input
元素的集合。@Pawel我还更新了我的答案,以提供一些额外的指导。我希望在显示结果后收集所有4个输入并采取行动。试图了解它是如何工作的。谢谢你的帮助help@Pawel没问题。你还需要什么帮助吗?
<html>
<head>
</head>
<body>
<div>
<h1>Calculator</h1>
<div>ingredient: 
<input type="number" id="x" placeholder="x">
<input type="number" id="y" placeholder="y">
<input type="number" id="z" placeholder="z">
<input type="number" id="a" placeholder="a"></div>

<button onclick="calo()" id="cal">Go</button>
<h1>Facts</h1>  
</div>
<div id="result">Result is displayed here.</div>
</body>
</html>


function calo() {
const [x,y,z,a] = [...document.querySelectorAll('input')];
document.querySelector("pre").textContent = x.outerHTML + "\n" + y.outerHTML + "\n" + z.outerHTML + "\n" + a.outerHTML;

var calculate = x + y + z + a;

document.getElementById("result").innerHTML = calculate;}
document.querySelectorAll('input')
    .forEach(input=>console.log("Id:", input.id, "Value:", input.value));