多个嵌套的if语句不计算

时间:2014-11-07 12:43:48

标签: javascript html if-statement

我正试图让这个工资单计算器运行多个相互嵌套的if语句。

需要满足的条件如下:

例如,如果月工资总额为2000 并且NI代码的最后一个字母是A

制定国民保险津贴,你用毛工资除以4。 2000 /乘4 = 500.意味着NI频段为a2。

总和则为“毛* a2”(如代码顶部列出的变量所示)

然后将结果显示为“你的NI演绎:??”

如果输入了另一个字母,我很难让if语句运行..例如“B”

    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

<script>
function computeNet() {
<!--NI Codes - use https://www.gov.uk/national-insurance-rates-letters/contribution-rates for updates on NI rates--!>
//NI Code A//
var a1 = 0;
var a2 = 0.12;
var a3 = 0.12;
var a4 = 0.02;
//NI Code B//
var b1 = 0;
var b2 = 0.0585;
var b3 = 0.0585;
var b4 = 0.02;
//NI Code C//
var c = 0;
var d1 = 1.4;
var d2 = 0.106;
var d3 = 0.12;
var d4 = 0.02;
//NI Code E//
var e1 = 0;
var e2 = 0.0585;
var e3 = 0.0585;
var e4 = 0.02;
//NI Code J//
var j1 = 0;
var j2 = 0.02;
var j3 = 0.02;
var j4 = 0.02;
//NI Code L//
var l1 = 1.4;
var l2 = 0.02;
var l3 = 0.02;
var l4 = 0.02;

var gross = document.getElementById('gross').value;
var tax_code = document.getElementById('tcode').value;
var ni_code = document.getElementById('ni').value;

var allowance = ((gross - ((tax_code * 10)/12)));
var ni_allowance = (gross / 4);


         if (ni_code = "A") {
             if (ni_allowance > 111 && ni_allowance < 153) {
                 ni_minus = (gross * a1);
             }
             else if (ni_allowance > 153.01 && ni_allowance < 770) {
                 ni_minus = (gross * a2);
             }
             else if (ni_allowance > 770.01 && ni_allowance < 805) {
                 ni_minus = (gross * a3);
             }
             else {
                 ni_minus = (gross * a4);
             }
        if (ni_code = "B") {
             if (ni_allowance > 111 && ni_allowance < 153) {
                 ni_minus = (gross * b1);
             }
             else if (ni_allowance > 153.01 && ni_allowance < 770) {
                 ni_minus = (gross * b2);
             }
             else if (ni_allowance > 770.01 && ni_allowance < 805) {
                 ni_minus = (gross * b3);
             }
             else {
                 ni_minus = (gross * b4);
             }
         }
         document.getElementById('ni_deduction').innerHTML = "Your NI deduction:"+ni_minus;
}
}
</script>
</head>

<body>
<p>Your Monthly Gross Pay: £<input id="gross" type="number" onChange="computeNet()"></p>
<p>The numerical part of your tax code: <input id="tcode" type="number" onChange="computeNet()"></p>
<p>last letter of your NI code: <input id="ni" type="text" onChange="computeNet()"></p>

<h2 id="gross_pay"></h2>    
<h2 id="paye-deduction"></h2>
<h2 id="ni_deduction"></h2>
<h2 id="net_pay"></h2>
</body>
</html>

此处代码:http://jsfiddle.net/n384noLk/

我期待着你的帮助

2 个答案:

答案 0 :(得分:1)

如果&#34; B&#34;需要移出If&#34; A&#34;。

因为它是嵌套的,所以你跳过If&#34; B&#34;完全声明如果ni_code不等于&#34; A&#34;。

if (ni_code == "A") {
    if (ni_allowance > 111 && ni_allowance < 153) {
        ni_minus = (gross * a1);
    } else if (ni_allowance > 153.01 && ni_allowance < 770) {
        ni_minus = (gross * a2);
    } else if (ni_allowance > 770.01 && ni_allowance < 805) {
        ni_minus = (gross * a3);
    } else {
        ni_minus = (gross * a4);
    }
} elseif (ni_code == "B") {
        if (ni_allowance > 111 && ni_allowance < 153) {
            ni_minus = (gross * b1);
        } else if (ni_allowance > 153.01 && ni_allowance < 770) {
            ni_minus = (gross * b2);
        } else if (ni_allowance > 770.01 && ni_allowance < 805) {
            ni_minus = (gross * b3);
        } else {
            ni_minus = (gross * b4);
        }
}

http://jsfiddle.net/n384noLk/2/

答案 1 :(得分:0)

在JavaScript中通过==测试平等,因此应该是:

if(ni_code == "A")

if(ni_code == "B")

此外,您没有关闭第一个if语句,因此它正在查看

if(ni_code == "B")

作为嵌套if的延续。你需要另一个}之后:

else {
    ni_minus = (gross * a4);
}