[英]Updating form input field value based on other inputs
I am trying to build a calculator of sorts where you an input all but one variable and get the final variable autofilled in the form.我正在尝试构建一种计算器,您可以在其中输入除一个变量之外的所有变量,并在表单中自动填充最终变量。 I am trying this using a javascript function oninput for the form.我正在尝试使用表单的 javascript function oninput。 However I am not sure how to achieve this.但是我不确定如何实现这一目标。 I have written a basic example of what I want below (though this does not work):我在下面写了一个我想要的基本示例(尽管这不起作用):
<form oninput="thermal()">
<input type="number" name="avgTemp" id="avgTemp" class="five-col">
<input type="number" name="volume" id="volume" class="five-col">
<input type="number" name="deltaTemp" id="deltaTemp" class="five-col">
<input type="number" name="Q" id="Q" class="five-col">
</form>
function thermal(){
var volume = document.getElementById("volume");
var deltaTemp= document.getElementById("deltaTemp");
value = deltaTemp;
}
function thermal(){
let avgTemp = document.getElementById("avgTemp").value;
let volume = document.getElementById("volume").value;
let deltaTemp = document.getElementById("deltaTemp").value;
let q = document.getElementById("Q") // no value here yet; we will make this our total
console.log(avgTemp, volume, deltaTemp, q)
}
Well, let's start with this.好吧,让我们从这个开始。 This gives you each input value individually - from here, we can implement the pertinent logic to combine input values into one input field.这会单独为您提供每个输入值 - 从这里,我们可以实施相关逻辑以将输入值组合到一个输入字段中。 I will make the Q
input our "total" just so you can get a picture of how combining may look.我将使Q
输入成为我们的“总计”,这样您就可以了解组合的外观。
function thermal(){
let avgTemp = document.getElementById("avgTemp").value;
let volume = document.getElementById("volume").value;
let deltaTemp = document.getElementById("deltaTemp").value;
let q = document.getElementById("Q");
let total = Number(avgTemp) + Number(volume) + Number(deltaTemp)
console.log(total)
q.value = total
}
I am not sure what you are looking for but this should give you a decent head start.我不确定您在寻找什么,但这应该会给您一个良好的开端。 Fire away if you have any questions.如果您有任何问题,请开火。
If you want to play around with it, here is the codepen: https://codepen.io/Pulse3358/pen/ExPZaVM如果你想玩它,这里是 codepen: https://codepen.io/Pulse3358/pen/ExPZaVM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.