将MM转换为英寸-Javascript

时间:2018-11-05 22:50:18

标签: javascript

如何将毫米换算成英寸?我正在尝试将MM转换为英寸。这是我的代码段。

function cmConverter() {
  document.converter.inch.value = document.converter.cm.value / 2.54
  document.converter.feet.value = document.converter.cm.value / 30.48
}

function inchConverter() {
  document.converter.cm.value = document.converter.inch.value * 2.54
  document.converter.feet.value = document.converter.inch.value / 12
}

function feetConverter() {
  document.converter.cm.value = document.converter.feet.value * 30.48
  document.converter.inch.value = document.converter.feet.value * 12
}
<form name="converter">
  Cm: <input type="text" name="cm" onChange="cmConverter()"><br /> 
  Inch: <input type="text" name="inch" onChange="inchConverter()"><br /> 
  Feet: <input type="text" name="feet" onChange="feetConverter()"><br />
 
  <input type="button" value="Convert!" />
</form>

3 个答案:

答案 0 :(得分:1)

在html中添加另一种输入类型。

  Mm: <input type="text" name="mm" onChange="mmConverter()"><br />

和JavaScript代码

 function mmConverter() {
  document.converter.inch.value = document.converter.mm.value / 25.4
  document.converter.feet.value = document.converter.mm.value / 304.8
  document.converter.cm.value = document.converter.mm.value / 10

}

答案 1 :(得分:1)

一厘米有10毫米,所以只需将厘米数除以10即可转换为毫米。

function cmConverter() {
  document.converter.inch.value = document.converter.cm.value / 2.54
  document.converter.feet.value = document.converter.cm.value / 30.48
  document.convert.mm.value = document.convert.cm.value/10;
}

function inchConverter() {
  document.converter.cm.value = document.converter.inch.value * 2.54
  document.converter.feet.value = document.converter.inch.value / 12
  document.converter.mm.value = document.convert.inch.value*25.4;
}

function feetConverter() {
  document.converter.cm.value = document.converter.feet.value * 30.48
  document.converter.inch.value = document.converter.feet.value * 12
   document.converter.mm.value = document.convert.feet.value*304.8;
} 
function mmConverter() {
  document.converter.inch.value = document.converter.mm.value / 25.4
  document.converter.feet.value = document.converter.mm.value / 304.8;
  document.converter.cm.value = document.converter.mm.value * 10
}
<form name="converter">
  Cm: <input type="text" name="cm" onChange="cmConverter()"><br /> 
  Inch: <input type="text" name="inch" onChange="inchConverter()"><br /> 
  Feet: <input type="text" name="feet" onChange="feetConverter()"><br />
  
 MM: <input type="text" name="mm" onChange="mmConverter()"><br />
  <input type="button" value="Convert!" />
</form>

答案 2 :(得分:0)

function convertMmToInches(){
    var mm = document.getElementById('milli').value;
    var inches = mm/25.4;
    inches = inches.toPrecision(10);
    document.getElementById('result').value = inches;
}
<html>
    <body>
        <input type="number" id="milli" min="0" onKeyUp="convertMmToInches()"/>
        <input type="number" id="result"/>
    </body>
</html>

如果您尝试做这样的事情-[MM in Inches],希望对您有所帮助!