我正在尝试将 Python 脚本转换为基于 HTML/Javascript Web 的转换器。该脚本非常简单,我按照https://www.w3schools.com/howto/howto_js_length_converter.asp上的步骤进行操作,如果所有字段都是动态的,那么效果很好。就我而言,我需要一个变量不改变。例如:用户提供“AssayWeight”值和以下之一:PPM 或 Grav。之后,脚本会根据 PPM/Grav 计算用户未提供的其他值。这是我到目前为止的代码:
<script>
function ppmtoGravConverter(source,valNum) {
valNum = parseFloat(valNum);
var inputPPMtoGrav = document.getElementById("inputPPMtoGrav");
var inputAssayWeight = document.getElementById("inputAssayWeight");
var inputGravtoPPM = document.getElementById("inputGravtoPPM");
if (source=="inputPPMtoGrav") {
inputGravtoPPM.value=(valNum*inputAssayWeight/1000).toFixed(4);
}
if (source=="inputGravtoPPM") {
inputPPMtoGrav.value=(valNum*1000/inputAssayWeight).toFixed(4);
}
}
</script>
和来自 w3schools 的 html:
<label>Assay Weight</label>
<input id="inputAssayWeight" type="number" placeholder="Assay Weight" oninput="ppmtoGravConverter(this.id,this.value)" onchange="ppmtoGravConverter(this.id,this.value)">
<br><br>
<label>PPM</label>
<input id="inputPPMtoGrav" type="number" placeholder="PPM" oninput="ppmtoGravConverter(this.id,this.value)" onchange="ppmtoGravConverter(this.id,this.value)">
<label>Grav (mg)</label>
<input id="inputGravtoPPM" type="number" placeholder="Grav (mg)" oninput="ppmtoGravConverter(this.id,this.value)" onchange="ppmtoGravConverter(this.id,this.value)">
请让我知道我错过了什么。这是我看 Javascript 的第二天。谢谢!
侃侃无极
一只甜甜圈
相关分类