zoukankan      html  css  js  c++  java
  • javascript实现的平方米、亩、公顷单位换算小程序

    <!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实现的平方米、亩、公顷单位换算小程序</title>
    </head>
    
    <body>
    
    <select onchange="selectChange(this)" id="sel">
    <option value="公顷">公顷</option>
    <option value="亩"></option>
    <option value="平方米">平方米</option>
    </select>
    这个input的值可能是3公顷、3亩、3平方米
    <input type="text" value="3" id="input0"/>
    <script type="text/javascript">
         var a = parseInt('0'); /////这里改为你动态接受到的值,0代表单位为平方米,1为亩,2为公顷
         var sel = document.getElementById('sel');
         sel.selectedIndex = 2 - a; /////设置单位下拉
         var lastUnit = document.getElementById('sel').value; //记录当前单位
         var input = document.getElementById("input0");
         //10000平米 = 15亩 = 1公顷
         var fRate = {//换算率
           公顷: { 亩: 15, 平方米: 10000 },
           亩: { 平方米: 10000 / 15, 公顷: 1 / 15 },
           平方米: { 亩: 15 / 10000, 公顷: 1 / 10000}
         };
         function selectChange(obj) {//单位改变,执行换算
           var v = parseFloat(input.value);//得到原来的值
           //执行换算,注意fRate的取值,得到上一次的单位节点,再取当前单位的换算率
           var rst = (v * fRate[lastUnit][sel.value]).toFixed(4);//保留4位小数
           input.value = rst;
           lastUnit = sel.value;//更新当前单位变量
         }
    </script>
    
    </body>
    </html>
    • <!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实现的平方米、亩、公顷单位换算小程序</title>
    • </head>
    • <body>
    • <select onchange="selectChange(this)" id="sel">
    • <option value="公顷">公顷</option>
    • <option value="亩"></option>
    • <option value="平方米">平方米</option>
    • </select>
    • 这个input的值可能是3公顷、3亩、3平方米
    • <input type="text" value="3" id="input0"/>
    • <script type="text/javascript">
    • var a = parseInt('0'); /////这里改为你动态接受到的值,0代表单位为平方米,1为亩,2为公顷
    • var sel = document.getElementById('sel');
    • sel.selectedIndex = 2 - a; /////设置单位下拉
    • var lastUnit = document.getElementById('sel').value; //记录当前单位
    • var input = document.getElementById("input0");
    • //10000平米 = 15亩 = 1公顷
    • var fRate = {//换算率
    • 公顷: { 亩: 15, 平方米: 10000 },
    • 亩: { 平方米: 10000 / 15, 公顷: 1 / 15 },
    • 平方米: { 亩: 15 / 10000, 公顷: 1 / 10000}
    • };
    • function selectChange(obj) {//单位改变,执行换算
    • var v = parseFloat(input.value);//得到原来的值
    • //执行换算,注意fRate的取值,得到上一次的单位节点,再取当前单位的换算率
    • var rst = (v * fRate[lastUnit][sel.value]).toFixed(4);//保留4位小数
    • input.value = rst;
    • lastUnit = sel.value;//更新当前单位变量
    • }
    • </script>
    • </body>
    • </html>
  • 相关阅读:
    TextView文字排版问题:
    Cent OS 6 主机名设置
    windows server 时间同步
    DELL服务器SAS 5 I_R 完全配置手册
    SAS 5/iR Adapter 驱动下载
    U盘加载硬盘控制卡驱动安装Windows 2003 指南
    邮件客户端导入邮件通讯录地址薄
    Symantec System Recovery
    windows server 备份与还原
    Acronis 备份使用
  • 原文地址:https://www.cnblogs.com/kuangwl/p/13447266.html
Copyright © 2011-2022 走看看