function selCalcType_onchange() { var calcType = getValueById("selCalcType"); if (calcType == 0) { document.getElementById("spanA").innerHTML = "年利率"; document.getElementById("spanB").innerHTML = "日利率"; document.getElementById("spanC").innerHTML = "月利率"; } else if (calcType == 1) { document.getElementById("spanA").innerHTML = "月利率"; document.getElementById("spanB").innerHTML = "日利率"; document.getElementById("spanC").innerHTML = "年利率"; } else { document.getElementById("spanA").innerHTML = "日利率"; document.getElementById("spanB").innerHTML = "月利率"; document.getElementById("spanC").innerHTML = "年利率"; } } function getValueById(elementId) { return document.getElementById(elementId).value; } var calc=function(){ var calcType = getValueById("selCalcType"); var val = $('#txtA').val(); if(!isIntNum(val)){ toastr.warning("输入有误"); return false; } var _numA=$('#txtA').val()/100; var numB = 0; var numC = 0; if (calcType == 0) { numB = _numA / 360; numC = _numA / 12; } else if (calcType == 1) { numB = _numA / 30; numC = _numA * 12; } else { numB = _numA * 30; numC = _numA * 360; } $("#txtB").val(FormatRate(numB)); $("#txtC").val(FormatRate(numC)); toastr.success("计算成功"); } function FormatRate(rate) { rate = rate * 100; var str = rate.toFixed(4); str = (str.substring(str.length - 1) == '0') ? str.substring(0, str.length - 1) : str str = (str.substring(str.length - 1) == '0') ? str.substring(0, str.length - 1) : str str = (str.substring(str.length - 1) == '0') ? str.substring(0, str.length - 1) : str str = (str.substring(str.length - 1) == '0') ? str.substring(0, str.length - 1) : str str = (str.substring(str.length - 1) == '.') ? str.substring(0, str.length - 1) : str return str; } function isIntNum(val){ var regPos = /^\d+$/; // 非负整数 var regNeg = /^\-[1-9][0-9]*$/; // 负整数 if(regPos.test(Number(val)) || regNeg.test(Number(val))){ return true; }else{ return false; } }