简体   繁体   English

如何使这种转换 function 双向工作?

[英]How do I make this conversion function work in both directions?

I'm new in JS and I have trouble to finish a converter only with inputs, I explain the problem, We have two input.我是 JS 的新手,我无法仅通过输入完成转换器,我解释了问题,我们有两个输入。 Meters and Feet.米和英尺。 when I transmit a number to Feet I have the result in Meters.当我将一个数字传输到 Feet 时,我的结果是 Meters。 And I Want to do the same think with Meters .我想对 Meters 做同样的想法。 and vice versa反之亦然

 let metresEl = document.getElementById('inputMetres'); function LengthConverter(valNum) { metresEl.value = valNum/3.2808; }
 <div class="container"> <div class="form-group"> <label>Feet</label> <input type="number" id="inputFeet" placeholder="Feet" oninput="LengthConverter(this.value)" onchange="LengthConverter(this.value)" > </div> <div class="form-group"> <label>Metres</label> <input type="number" id="inputMetres" placeholder="Metres"> </div> </div>

You can add another parameter in the LengthConvertor function which will say the input unit (meter or feet) and convert it accordingly inside the function using if .您可以在LengthConvertor function 中添加另一个参数,该参数将表示输入单位(米或英尺)并在 function 中使用if进行相应的转换。

function LengthConverter(valNum, inputUnit) {
   if(inputUnit === 'feet')
    metresEl.value = valNum/3.2808;
   if(inputUnit === 'meter')
    feetsEL.value = valNum * 3.2808;
}


<div class="container">
    <div class="form-group">
      <label>Feet</label>
      <input type="number" id="inputFeet" placeholder="Feet" oninput="LengthConverter(this.value,"feet")" onchange="LengthConverter(this.value,"feet")" >
    </div>
    <div class="form-group">
      <label>Metres</label>
      <input type="number" id="inputMetres" placeholder="Metres" oninput="LengthConverter(this.value,"meter")" onchange="LengthConverter(this.value,"meter")" >
    </div>
  </div>

Added inverse conversion:添加了逆变换:

 let metresEl = document.getElementById('inputMetres'); let feetEl = document.getElementById('inputFeet'); function FeetToMetres(valNum) { metresEl.value = valNum/3.2808; } function MetresToFeet(valNum) { feetEl.value = 3.2808*valNum; }
 <div class="container"> <div class="form-group"> <label>Feet</label> <input type="number" id="inputFeet" placeholder="Feet" oninput="FeetToMetres(this.value)" onchange="FeetToMetres(this.value)" > </div> <div class="form-group"> <label>Metres</label> <input type="number" id="inputMetres" placeholder="Metres" oninput="MetresToFeet(this.value)" onchange="MetresToFeet(this.value)"> </div> </div>

You can add a element.addEvenetListener to each input, and when it chances you get it's value, convert, and put in on the right input.您可以将element.addEvenetListener添加到每个输入,当您有机会获得它的值时,转换并放入正确的输入。

let metresEl = document.getElementById('inputMetres');
let feetsEl = document.getElementById('inputFeets');

metresEl.addEventListener('change', yourCode);
feetsEl.addEventListener('change', yourCode);

For exemple, if metres input changes, you convert to feets and add to feets input.例如,如果米输入发生变化,则转换为英尺并添加到英尺输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM