js 页面填写数字,自动计算

来源:互联网 发布:亚太会计师事务所 知乎 编辑:程序博客网 时间:2024/05/17 08:06

1. input 填写数值,并且自动计算。

页面

<table><tr><td>邮费<div>Postage(RBM)</div></td><td><input type="text" id="postage" name="postage" onkeyup="value=value.replace(/[^\d\.]/g,'');SumNum();" /></td></tr>                <tr><td>其他费用<div>Package Fee</div></td><td><input type="text" id="otherFee" name="otherFee" onkeyup="value=value.replace(/[^\d\.]/g,'');SumNum();" /></td><td>合计费用<div>Total Fee</div></td><td><input type="text" id="totalFee" name="totalFee" /></td></tr></table>

js

// for the totalFee count autofunction  SumNum(){ var sumValue; postage=eval(document.getElementById('postage').value); otherFee=eval(document.getElementById("otherFee").value);if(postage==null){postage=0;}if(otherFee==null){otherFee=0;}sumValue = postage+otherFee;$("#totalFee").attr("value",sumValue);}

js 关于是否number类型校验,本来是用 isNaN(), 但是一直抱错。