简体   繁体   English

我找不到为什么这个简单的JS无法使用。 (销售税计算器)

[英]I cannot find why this simple JS isn't working. (Sales Tax Calculator)

My HTML is a simple entry form like this: 我的HTML是一个简单的输入表单,如下所示:

<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 (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" onclick="calculate_click()">
    <input type="button" id="clear" value="Clear" onclick="clear_click()">

And my JS is 2 simple functions... 我的JS是2个简单的函数...

    var subtotal, taxRate, salesTax, total;

function setValues() {
    subtotal = document.getElementById("subtotal").value; 
    taxRate = document.getElementById("tax_rate").value; 
    salesTax = document.getElementById("sales_tax").value;  
    total = document.getElementById("total").value; 
}

function calculate_click() {
    setValues();
    salesTax = subtotal * taxRate / 100;
    total = subtotal + salesTax;

}

I have included a JS Fiddle link for more information as well: http://jsfiddle.net/tjhillard/nkHxe/1/ I want the sales tax and the total to display inside the appropriate fields when the "Calculate" button is clicked. 我还包括了JS Fiddle链接,以获取更多信息: http : //jsfiddle.net/tjhillard/nkHxe/1/当单击“计算”按钮时,我希望在相应的字段中显示营业税和总计。

Thank you in advance for your help! 预先感谢您的帮助!

you just need to update the values back: 您只需要更新回值即可:

change this: 改变这个:

function calculate_click() {
    setValues();
    salesTax = subtotal * taxRate / 100;
    total = subtotal + salesTax;    
}

into: 成:

function calculate_click() {
    setValues();
    salesTax = (subtotal * taxRate) / 100;
    total = subtotal + salesTax;   

    // place the value in the form
    document.getElementById("sales_tax").value = salesTax;
    document.getElementById("total").value = total; 
}

updated version of your code: http://jsfiddle.net/nkHxe/4/ 您的代码的更新版本: http : //jsfiddle.net/nkHxe/4/

It should be in the following way: 应该采用以下方式:

function calculate_click() {
    setValues();
    salesTax = subtotal * taxRate / 100;
    total = subtotal + salesTax;

    document.getElementById("sales_tax").value = salesTax;
    document.getElementById("total").value = total;
}

http://jsfiddle.net/nkHxe/5/ http://jsfiddle.net/nkHxe/5/

PS Keep in mind that your code is bad-organized )) PS请记住,您的代码是错误组织的))

1) You don't write back the values. 1)您不回写值。 Maxim and Balexandre already showed how. Maxim和Balexandre已经展示了方法。

2) total is a concatenation of a string with a number which leads to a string again. 2) total是一个字符串与一个数字的连接,该数字又导致一个字符串。 This has not been solved by Maxim and Balexandre. Maxim和Balexandre尚未解决此问题。

By directly reading from the input field, you get strings back. 通过直接从输入字段读取,可以返回字符串。 So, parse them first into numbers. 因此,首先将它们解析为数字。 Example: 例:

parseInt(document.getElementById("subtotal").value)

In your code, you have the issue when calculating total as explained above. 在您的代码中,如上所述存在计算total时的问题。 The issue does not occur for salesTax because the strings are automatically converted thanks to the math operations you apply. 对于salesTax不会发生此问题,因为salesTax您应用的数学运算,字符串将自动转换。

Hope this helps. 希望这可以帮助。

Updated, Fully Working, total code Fixed where Integers were Passed (Concatenated) as Strings. 已更新,已完全正常运行,总代码已修复将整数作为字符串传递(串联)的问题。

Code added , 添加了代码

subtotal = parseInt(document.getElementById("subtotal").value)
total = parseInt(document.getElementById("total").value)

Link , http://jsfiddle.net/nkHxe/150/ 链接http://jsfiddle.net/nkHxe/150/

Thanks to TJH, Balexandre, Maxim and Patrick :) 感谢TJH,Balexandre,Maxim和Patrick :)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM