javascript - Javascript Tax Calculator Input boxes not Functioning
问题描述
this is a homework assignment in which I am supposed to write the JavaScript function (calculate) to calculate and display sales tax and total upon hitting the calculate button.
This was provided for me:
salesTax = subtotal * taxRate/100;
total = subtotal + salesTax;
and
Must be a positive number less than 10000000
Must be a positive number less than 50
Here is my code, can you tell me why it is not functioning? I am a novice obviously:
<!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");
试试这个,看看它是否有效。
推荐阅读
- c++ - Opencv 的 cv::Mat::forEach<> 函数的 position 参数对应什么索引顺序?
- ruby-on-rails - 在 Rails 6 中软删除活动文本
- javascript - Bootstrap 4 崩溃不适用于 ReactJS 中的地图
- python - 使用python从Linux上的特定内存地址读取值
- flutter - 解析 Json 数组和对象 Flutter
- java - 在 Spring JPA 中使用 SortedSet
- blockchain - 谁应该在区块链上部署智能合约?
- tsql - ElasticSearch 中的多字段通配符搜索
- javascript - 从数据库中获取值并在 Laravel 中单击按钮时以表单形式显示它
- c# - Azure 服务总线订阅。关闭()未按预期工作