javascript實現的平方米、畝、公頃單位換算小程式_javascript技巧

來源:互聯網
上載者:User

javascript實現平方米,畝,公頃單位換算,可以通過url傳遞參數指定輸入框的值為任何中單位的值。

原始碼如下:

<!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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.