【问题标题】:Javascript Tax Calculator Input boxes not FunctioningJavascript税收计算器输入框不起作用
【发布时间】:2021-10-12 23:32:24
【问题描述】:

这是一个家庭作业,我应该在其中编写 JavaScript 函数 (calculate) 来计算并在点击计算按钮时显示销售税和总计。

这是为我提供的:

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>&nbsp;</label>
            <input type="button" id="calculate" value="Calculate">
            <input type="button" id="clear" value="Clear"><br />
        </div>
    </div>
</body>

</html>

【问题讨论】:

  • 它怎么不工作了?你有任何错误吗?预期输出与您所看到的相比是什么?

标签: javascript html calculator tax


【解决方案1】:

我已经运行了你的代码,但遇到了一个导致它停止运行的问题:

            //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");

试试这个,看看它是否有效。

【讨论】:

    【解决方案2】:

    您正在检查错误的条件 -

    if (subtotal <= 10000000 || tax_rate <= 50)
    

    应该是以下条件-

    if (subtotal >= 10000000 || tax_rate >= 50)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多