不起作用的数字之间的差异javascript

时间:2014-07-05 11:18:48

标签: javascript jquery html css

这是一个代码部分,当我更改扣除值时,如果值恰好正确,则手动更改净总值,值的颜色将更改为绿色,否则将更改为红色。 但是在此代码中,当扣除值的范围从50到99时,颜色不会变为绿色,此时粗略和扣除之间的差异是正确的。

我该如何解决这个问题?帮帮我

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
        <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script>
function calculatenet(str,str1)
 {
    var id = str1.split('-');
    var cid = id[3];
    var num = id[2].split('net');
    var gross = $("#income-self-gincome"+num[1]+"-"+cid).val();
    var deduct= $("#income-self-deduct"+num[1]+"-"+cid).val();
        if(gross >= deduct)
          {
            var net = parseInt(gross - deduct);

            if(net==str)
              {
                $("#income-self-net"+num[1]+"-"+cid).css('color','green');
              }
            else
              {
                $("#income-self-net"+num[1]+"-"+cid).css('color','red');
              }
          }
        else
          {
            $("#income-self-net"+num[1]+"-"+cid).css('color','red');
          }



        }
</script>
</head>

<body>
<table>
<tr>
                          <td>Gross Total Income.</td>
                          <td><input type="text"  name="income-self-gincome1-1" id="income-self-gincome1-1" style="width:100px" /></td>

                          <td><input type="text"  name="income-self-gincome2-1" id="income-self-gincome2-1" style="width:100px" /></td>

                          <td><input type="text"  name="income-self-gincome3-1" id="income-self-gincome3-1" style="width:100px" /></td>

                          </tr>
                           <tr>
                          <td>Deduction</td>
                          <td><input type="text" name="income-self-deduct1-1" id="income-self-deduct1-1" style="width:100px" /></td>

                          <td><input type="text" name="income-self-deduct2-1" id="income-self-deduct2-1" style="width:100px" /></td>

                          <td><input type="text" name="income-self-deduct3-1" id="income-self-deduct3-1" style="width:100px" /></td>

                          </tr>
                          <tr>
                          <td>Net Total Income</td>
                          <td><input type="text" onKeyUp="calculatenet(this.value,this.id)" name="income-self-net1-1" id="income-self-net1-1" style="width:100px" /></td>

                          <td><input type="text" onKeyUp="calculatenet(this.value,this.id)" name="income-self-net2-1" id="income-self-net2-1" style="width:100px" /></td>

                          <td><input type="text" onKeyUp="calculatenet(this.value,this.id)" name="income-self-net3-1" id="income-self-net3-1" style="width:100px" /></td>

                          </tr>
                          </table>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

改变这个:

var net = parseInt(gross - deduct);

为:

var net = parseInt(gross) - parseInt(deduct);

因为你需要在对它们执行算术运算之前将它们都转换为整数。

修改

你也没有将grossdeduct解析为int所以它不会进入条件if块:

 var gross = parseInt($("#income-self-gincome"+num[1]+"-"+cid).val());
            var deduct= parseInt($("#income-self-deduct"+num[1]+"-"+cid).val());

Working FIDDLE