我以前用類似的方式發佈了一個代碼,我仍然有問題。這次我得到100.0712.5
當我把10
作爲帳單,7
爲銷售稅和25
爲小費。我對Javascript編碼真的很陌生,而且我幾乎花了數小時試圖弄清楚我需要幫助。銷售和小費計算器
<html>
<head>
<script type="text/javascript">
function applyTax(){
var inputAmount = document.getElementById('dollars').value;
var salesTax = document.getElementById('tax').value;
var tip = document.getElementById('tip').value;
var totalAmount = (salesTax/100) + (inputAmount);
var tipprcnt = (tip/100) * (inputAmount);
var Grandtotal = (inputAmount + (totalAmount*1) + (tipprcnt*1));
//document.getElementById('requestedAmount').innerHTML = tipprcnt;
//document.getElementById('requestedTax').innerHTML = totalAmount;
document.getElementById('requestedGrand').innerHTML = Grandtotal;
}
</script>
</head>
<body>
<h1>Sales Tax + Tip Calculator</h1>
<p>Type in your price (dollar amount). Click the "Calculate" button to receive your total.
</p>
<p>
What is the bill amount?: $<input type="text" id="dollars" /> <br>
What is the sales tax?:<input type="text" id="tax" />%<br>
how much do you want to tip?:<input type="text" id="tip" />%
<input type="button" onclick="applyTax();" value="Calculate" />
</p>
</h2>The Grand Total is:</h2>
<div id="requestedAmount"> </div>
<div id="requestedTax"> </div>
<div id="requestedGrand"> </div>
<p><a href="http://jasonhuang.myweb.usf.edu/lis4365/index.html">Home</a>
</body>
</html>
你將稅額添加到輸入數量而不是乘以它。 – Barmar 2014-09-04 22:58:38
@Bijan他這樣做是爲了將它從一個字符串轉換爲一個數字,所以'+'會做加法而不是連接。除了需要轉換的是'inputAmount'。 – Barmar 2014-09-04 23:04:19