Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不计算增值税的增值税应用程序_Javascript_Html - Fatal编程技术网

Javascript 不计算增值税的增值税应用程序

Javascript 不计算增值税的增值税应用程序,javascript,html,Javascript,Html,我正在阅读《Murcah的Javascript和DOM脚本》一书。我正在学习第一个示例,它创建了一个销售税应用程序 这是我的小提琴,让我知道我做错了什么: HTML <!DOCTYPE html> <HTML> <HEAD> <TITLE>Sales Tax Calculator</TITLE> <link rel="stylesheet" type="tect/css" href="s

我正在阅读《Murcah的Javascript和DOM脚本》一书。我正在学习第一个示例,它创建了一个销售税应用程序

这是我的小提琴,让我知道我做错了什么:

HTML

<!DOCTYPE html>
<HTML>

    <HEAD>
        <TITLE>Sales Tax Calculator</TITLE>
        <link rel="stylesheet" type="tect/css" href="sales_tax.css" />
        <script type="text/javascript" src="sales_tax.js"></script>
    </HEAD>

    <BODY>
        <DIV id="content">
            <h1>Sales Tax Calculator</h1>

            <p>Enter the values below and click "Calculate".</p>
            <div id="taxCalc">
                <label for="subtotal">Subtotal:</label>
                <input type="text" id="subtotal" />
                <br/>
                <label for="taxRate">Tax Rate:</label>
                <input type="text" id="taxRate" />%
                <br/>
                <label for="salesTax">Sales Tax:</label>
                <input type="text" id="salesTax" disabled="disabled" />
                <br/>
                <label for="total">Total:</label>
                <input type="text" id="total" disabled="disabled" />
                <br/>
                <label>&nbsp;</label>
                <input type="button" id="calculate" value="Calculate" />
                <br/>
            </div>
        </DIV>
    </BODY>

</HTML>
JavaScript

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

var calculate_click = function () {
    var subtotal = parseFloat($("subtotal").value);
    var taxRate = parseFloat($("taxRate").value);

    $("salesTax").value = "";
    $("total").value = "";

    if (isNaN(subtotal) || subtotal < 0) {
        alert("Subtotal must be a number that is zero or more!");
    } else if (isNan(taxRate) || taxRate < 0) {
        alert("Tax Rate must be a number that is zero or more!");
    } else {
        var salesTax = subtotal * (taxRate / 100);
        salesTax = parseFloat(salesTax.toFixed(2));
        var total = subtotal + salesTax;
        $("salesTax").value = salesTax;
        $("total").value = total.toFixed(2);
    }
}

window.onload = function () {
    $("calculate").onclick = calculate_click;
    $("subtotal").focus();
}
var$=函数(id){
返回文档.getElementById(id);
}
var计算\单击=函数(){
var小计=parseFloat($(“小计”).value);
var taxRate=parseFloat($(“taxRate”).value);
$(“销售税”)。价值=”;
$(“总额”)。价值=”;
如果(isNaN(小计)| |小计<0){
警报(“小计必须是零或更多的数字!”);
}else if(isNan(税率)| |税率<0){
警报(“税率必须是零或更多的数字!”);
}否则{
var salesTax=小计*(税率/100);
salesTax=parseFloat(salesTax.toFixed(2));
var总额=小计+销售税;
$(“销售税”)。价值=销售税;
$(“总计”).value=toFixed的总计(2);
}
}
window.onload=函数(){
$(“计算”).onclick=calculate\u click;
$(“小计”).focus();
}

您的代码有几个问题:

(1) 在JSFIDLE中,您已经在使用
window.onload
,因此在左侧窗格中不需要指定onload,只需将代码包装在头部或主体中即可。另外,在JSFIDLE中不需要包含
html
head
以及所有这些内容

(2)
isNan
不是一个函数。使用
isNaN

更改此项:
else if(isNan(taxRate)| | taxRate<0)

收件人:
else if(isNaN(税率)| |税率<0)


检查这个更新的小提琴:

不是最好的答案,但我在小提琴中修复了代码

var$=函数(id){
返回文档.getElementById(id);
}
var计算\单击=函数(){
var小计=parseFloat($(“小计”).value);
var taxRate=parseFloat($(“taxRate”).value);
$(“销售税”)。价值=”;
$(“总额”)。价值=”;
如果(isNaN(小计)| |小计<0){
警报(“小计必须是零或更多的数字!”);
}else if(isNaN(税率)| |税率<0){
警报(“税率必须是零或更多的数字!”);
}否则{
var salesTax=小计*(税率/100);
salesTax=parseFloat(salesTax.toFixed(2));
var总额=小计+销售税;
$(“销售税”)。价值=销售税;
$(“总计”).value=toFixed的总计(2);
}
}
$(“计算”).onclick=function(){calculate_click();};
$(“小计”).focus();

首先,您要包括脚本认为在jsfiddles服务器上的文件,如sales_tax.js感谢您清晰的说明。
var $ = function (id) {
    return document.getElementById(id);
}

var calculate_click = function () {
    var subtotal = parseFloat($("subtotal").value);
    var taxRate = parseFloat($("taxRate").value);

    $("salesTax").value = "";
    $("total").value = "";

    if (isNaN(subtotal) || subtotal < 0) {
        alert("Subtotal must be a number that is zero or more!");
    } else if (isNan(taxRate) || taxRate < 0) {
        alert("Tax Rate must be a number that is zero or more!");
    } else {
        var salesTax = subtotal * (taxRate / 100);
        salesTax = parseFloat(salesTax.toFixed(2));
        var total = subtotal + salesTax;
        $("salesTax").value = salesTax;
        $("total").value = total.toFixed(2);
    }
}

window.onload = function () {
    $("calculate").onclick = calculate_click;
    $("subtotal").focus();
}
var $ = function (id) {
    return document.getElementById(id);
}

var calculate_click = function () {
    var subtotal = parseFloat($("subtotal").value);
    var taxRate = parseFloat($("taxRate").value);

    $("salesTax").value = "";
    $("total").value = "";

    if (isNaN(subtotal) || subtotal < 0) {
        alert("Subtotal must be a number that is zero or more!");
    } else if (isNaN(taxRate) || taxRate < 0) {
        alert("Tax Rate must be a number that is zero or more!");
    } else {
        var salesTax = subtotal * (taxRate / 100);
        salesTax = parseFloat(salesTax.toFixed(2));
        var total = subtotal + salesTax;
        $("salesTax").value = salesTax;
        $("total").value = total.toFixed(2);
    }
}
$("calculate").onclick = function(){calculate_click();};
$("subtotal").focus();