简体   繁体   English

我的 javascript 的问题。 错误:分配了一个值但从未使用过。 [no-unused-var]

[英]Issues with my javascript. Error: is assigned a value but never used. [no-unused-var]

Trying to make a sales tax calculator.尝试制作销售税计算器。 I got it to calculate everything, but there are errors in my functions that I made to clear the textboxes.我用它来计算所有内容,但是我用来清除文本框的函数存在错误。 Any help would be appreciated, still a noob with html/css/js.任何帮助将不胜感激,仍然是 html/css/js 的菜鸟。 Basically in Brackets I'm seeing that for each function in my .js file, my functions arent actually being called.基本上在 Brackets 中,我看到对于我的 .js 文件中的每个函数,我的函数实际上并没有被调用。 An error pops up for each of them saying "is assigned a value but never used".他们每个人都会弹出一个错误,说“被分配了一个值但从未使用过”。

 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>

i don't see any errors in your code when I run it on code pen.当我在代码笔上运行它时,我没有看到您的代码中有任何错误。 the only thing is that textClearer doesn't exist.唯一的问题是 textClearer 不存在。 I changed it to clear_click and it worked.我将其更改为 clear_click 并且它起作用了。

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

Your clear_click variable function is never used.您的clear_click变量函数从未使用过。 You are assigning it's value by making it a function variable, but I'm not seeing it being used like clear_click() .您通过使其成为函数变量来分配它的值,但我没有看到它像clear_click()一样被使用。

The code by itself works as you posted it: https://codepen.io/anon/pen/jJYoww代码本身在您发布时起作用: https : //codepen.io/anon/pen/jJYoww

is assigned a value but never used

This is really more of a warning than an error, and should only be disruptive if you're using a linter or something.这实际上更像是一个警告而不是错误,并且只有在您使用 linter 或其他东西时才应该是破坏性的。

As @Phil asked, where are you seeing the errors, and are you doing anything else to this aside from what is posted?正如@Phil 所问的那样,您在哪里看到错误,除了发布的内容之外,您是否还对此进行了其他任何处理?

You may need to change function name clear_click to textClearer您可能需要将函数名称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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 11:5 错误“路由器”被分配了一个值,但从未使用过 no-unused-vars - 11:5 error 'router' is assigned a value but never used no-unused-vars 错误:“注释”被分配了一个值,但从未使用过-Javascript - Error: 'comment' is assigned a value but never used - Javascript 如何在没有ESLint no-unused-var错误的情况下公开全局javascript函数? - How do you expose a global javascript function without ESLint no-unused-var error? 反应错误“ ./src/App.js第7行:&#39;APIKEY&#39;被分配了一个值,但从未使用过no-unused-vars” - React error “./src/App.js Line 7: 'APIKEY' is assigned a value but never used no-unused-vars” &#39;inStock&#39; 被赋值但从未使用过 no-unused-vars - 'inStock' is assigned a value but never used no-unused-vars Vue.js 错误“app' is assigned a value but never used no-unused-vars - Vue.js error "app' is assigned a value but never used no-unused-vars Eslint No-unused-var用于装饰工厂 - Eslint no-unused-var for decorator factory 在内联函数中使用时,为什么会出现“ no-unused-var”错误 - Why am I getting a 'no-unused-var' error when using in an inline function 如何使用谷歌地图解决&#39;no-unused-var&#39;的ESLint错误 - how can i solve ESLint error that 'no-unused-var' with google map 一个从未使用过的未使用值,但在 React 中是 - An unused Value that is never used, but Is, in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM