我的JavaScript问题。错误:已分配值,但从未使用过。 [no-unused-var]

时间:2019-03-14 04:04:52

标签: javascript html css

尝试制作营业税计算器。我可以用它来计算所有内容,但是在清除文本框时我的函数中存在错误。任何帮助将不胜感激,仍然是html / css / js的菜鸟。基本上在括号中,我看到对于.js文件中的每个函数,实际上都没有调用我的函数。每个用户都会弹出一个错误消息,提示“已分配值但从未使用过”。

var $ = function(id)
{
  return document.getElementById(id);
}

var processEntries = function()
{
  var inpSubTotal = parseFloat($("subtotal").value);
  var inpTaxRate = parseFloat($("tax_rate").value);
  if (inpSubTotal <= 0 || subtotal > 10000)
  {
    $("subTotalsMessage").innerHTML = "Subtotal must be > 0 and < 10000";
  }

  if (inpTaxRate <= 0 || inpTaxRate > 12)
  {
    $("taxRateMsg").innerHTML = "Tax rate must be > 0 and < 12";
  }

  var calTax = inpSubTotal * (inpTaxRate / 100);
  calTax = parseFloat(calTax.toFixed(2));
  var totAmt = inpSubTotal + calTax;

  $("sales_tax").value = calTax;

  $("total").value = totAmt.toFixed(2);
}

var clear_click = function()
{
  $("subtotal").value = "";
  $("tax_rate").value = "";
  $("total").value = "";
  $("sales_tax").value = "";
}

function clearSubTotal()
{
  $("subtotal").value = "";
}

function clearTaxRate()
{
  $("tax_rate").value = "";
}

window.onload = function()
{
  $("calculate").onclick = processEntries;
}
<!DOCTYPE html>

<html>

<head>
  <meta charset="utf-8">
  <title>Sales Tax Calculator</title>
  <link rel="stylesheet" href="styles.css" />



  <script src="sales_tax.js"></script>
</head>

<body>
  <div id="content">
    <h1>Sales Tax Calculator</h1>

    <p>Enter Subtotal and Tax Rate and click "Calculate".</p>

    <div id="myTaxCal">
      <label for="subtotal">Subtotal:</label>
      <input type="text" id="subtotal" onclick="clearSubTotal()">
      <span id="subTotalsMessage">Enter order subtotal</span><br />

      <label for="tax_rate">Tax Rate:</label>
      <input type="text" id="tax_rate" onclick="clearTaxRate()">
      <span id="taxRateMsg">Enter sales tax rate (99.9)</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" onclick=textClearer()><br />

    </div>
  </div>
</body>

</html>

4 个答案:

答案 0 :(得分:1)

在代码笔上运行代码时,我看不到任何错误。唯一的事情是textClearer不存在。我将其更改为clear_click并成功了。

<input type="button" id="clear" value="Clear" onclick=clear_click()><br />

答案 1 :(得分:0)

您的clear_click变量函数从不使用。您正在通过使它成为一个函数变量来为其赋值,但是我没有看到它像clear_click()那样被使用。

答案 2 :(得分:0)

代码在您发布时就可以正常工作:https://codepen.io/anon/pen/jJYoww

is assigned a value but never used

这实际上是警告而不是错误,并且仅在使用短绒棉或其他东西时才具有破坏性。

正如@Phil所问的那样,您在哪里看到错误,除了发布的内容之外,您还在做其他事情吗?

答案 3 :(得分:0)

您可能需要将函数名称clear_click更改为textClearer

var $ = function(id) {
  return document.getElementById(id);
}

var processEntries = function() {
  var inpSubTotal = parseFloat($("subtotal").value);
  var inpTaxRate = parseFloat($("tax_rate").value);
  if (inpSubTotal <= 0 || subtotal > 10000) {
    $("subTotalsMessage").innerHTML = "Subtotal must be > 0 and < 10000";
  }

  if (inpTaxRate <= 0 || inpTaxRate > 12) {
    $("taxRateMsg").innerHTML = "Tax rate must be > 0 and < 12";
  }

  var calTax = inpSubTotal * (inpTaxRate / 100);
  calTax = parseFloat(calTax.toFixed(2));
  var totAmt = inpSubTotal + calTax;

  $("sales_tax").value = calTax;

  $("total").value = totAmt.toFixed(2);
}

var textClearer = function() {
  $("subtotal").value = "";
  $("tax_rate").value = "";
  $("total").value = "";
  $("sales_tax").value = "";
}

function clearSubTotal() {
  $("subtotal").value = "";
}

function clearTaxRate() {
  $("tax_rate").value = "";
}

window.onload = function() {
  $("calculate").onclick = processEntries;
}
<div id="content">
  <h1>Sales Tax Calculator</h1>

  <p>Enter Subtotal and Tax Rate and click "Calculate".</p>

  <div id="myTaxCal">
    <label for="subtotal">Subtotal:</label>
    <input type="text" id="subtotal" onclick="clearSubTotal()">
    <span id="subTotalsMessage">Enter order subtotal</span><br />

    <label for="tax_rate">Tax Rate:</label>
    <input type="text" id="tax_rate" onclick="clearTaxRate()">
    <span id="taxRateMsg">Enter sales tax rate (99.9)</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" onclick=textClearer()><br />

  </div>
</div>