[英]Javascript Tax Calculator Input boxes not Functioning
這是一個家庭作業,我應該在其中編寫 JavaScript 函數(計算)以在點擊計算按鈕時計算和顯示銷售稅和總計。
這是為我提供的:
salesTax = 小計 * taxRate/100;
總計 = 小計 + 銷售稅;
和
必須是小於 10000000 的正數
必須是小於 50 的正數
這是我的代碼,你能告訴我為什么它不起作用嗎? 我顯然是個新手:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sales Tax Calculator</title>
<link rel="stylesheet" href="style.css" />
<script>
//function declaration for calculating sales tax
function calculate() {
//get three inputs from input boxes in the form
let subtotal = parseInt(document.getElementById("subtotal").value);
let taxRate = parseFloat(document.getElementById("tax_rate").value);
//validation data
if (subtotal <= 10000000 || tax_rate <= 50) {
alert("sales tax exceeds 10000000 and/ or tax rate exceeds 50");
} else {
//calculate sales tax
salesTax = subtotal * taxRate / 100;
total = subtotal + salesTax;
//display the results to the last two boxes
document.getElementById("sales_tax").value = salesTax;
document.getElementById("total").value = total;
}
}
//function calling part
//function named clear_form to clear every data entry in the clear_form
//create function
function clear_form() {
document.getElementById("subtotal").value = " ";
document.getElementById("tax_rate").value = " ";
document.getElementById("sales_tax").value = " ";
document.getElementById("total").value = " ";
/*document.getElementById("investment").focus();*/
}
//function calling part using window.onload
window.onload = function() {
document.getElementById("calculate").onclick = calculate
document.getElementById("clear").onclick = clear_form
}
</script>
</head>
<body>
<div id="content">
<h1>Sales Tax Calculator</h1>
<p>Enter Subtotal and Tax Rate and click "Calculate".</p>
<div id="taxCalc">
<label for="subtotal">Subtotal:</label>
<input type="text" id="subtotal">
<span id="subtotal_message">Enter Order Subtotal</span><br />
<label for="tax_rate">Tax Rate:</label>
<input type="text" id="tax_rate">
<span id="tax_rate_message">Enter Sales Tax Rate </span><br />
<label for="sales_tax">Sales Tax:</label>
<input type="text" id="sales_tax" disabled><br />
<label for="total">Total:</label>
<input type="text" id="total" disabled><br />
<label> </label>
<input type="button" id="calculate" value="Calculate">
<input type="button" id="clear" value="Clear"><br />
</div>
</div>
</body>
</html>
我已經運行了您的代碼,但遇到了一個問題,使其無法運行:
//validation data
if (subtotal <= 10000000 || tax_rate <= 50) {
alert("sales tax exceeds 10000000 and/ or tax rate exceeds 50");
你不小心混合了大於和小於符號!
它應該是:
//validation data
if (subtotal >= 10000000 || tax_rate >= 50) {
alert("sales tax exceeds 10000000 and/ or tax rate exceeds 50");
試試這個,看看它是否有效。
你正在檢查錯誤的條件 -
if (subtotal <= 10000000 || tax_rate <= 50)
應該是以下條件——
if (subtotal >= 10000000 || tax_rate >= 50)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.