Javascript to achieve square meters mu ha unit conversion procedures

  • 2020-03-30 03:41:18
  • OfStack

Javascript to achieve square meters, mu, ha unit conversion, you can pass parameters through the url to specify the value of the input box to the value of any unit.

The source code is as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript The realization of square meters, mu, ha unit conversion procedures </title>
</head>

<body>

<select onchange="selectChange(this)" id="sel">
<option value=" ha "> ha </option>
<option value=" mu "> mu </option>
<option value=" Square meters "> Square meters </option>
</select>
 this input The value of theta could be zero 3 Ha, 3 Mu, 3 Square meters 
<input type="text" value="3" id="input0"/>
<script type="text/javascript">
     var a = parseInt('0'); ///// here change to the value you receive dynamically, 0 for square meters, 1 for mu, and 2 for hectare
     var sel = document.getElementById('sel');
     sel.selectedIndex = 2 - a; ///// sets the unit to drop down
     var lastUnit = document.getElementById('sel').value; //Record current unit
     var input = document.getElementById("input0");
     //10000 square meters = 15 mu = 1 hectare
     var fRate = {//Conversion rate
        ha : {  mu : 15,  Square meters : 10000 },
        mu : {  Square meters : 10000 / 15,  ha : 1 / 15 },
        Square meters : {  mu : 15 / 10000,  ha : 1 / 10000}
     };
     function selectChange(obj) {//Unit change, perform conversion
       var v = parseFloat(input.value);//I get the original value
       // Perform the conversion. Note fRate To get the last unit node, and then to the current unit Conversion rate
       var rst = (v * fRate[lastUnit][sel.value]).toFixed(4);//Keep four decimal places
       input.value = rst;
       lastUnit = sel.value;//Update the current unit variable
     }
</script>

</body>
</html>


Related articles: