如何计算薪金税

时间:2019-05-28 10:22:48

标签: javascript jquery

我正在从事一个工资项目,我想计算加班费,津贴和薪金税。我想给工资加班和津贴,并从工资中减去税款。

以下脚本可用于加班和津贴,但我在计算税额时遇到问题。

当我输入税百分比时,应该计算从薪水到税额的税额,并计算总额。当我直接输入税款时,应该计算taxPercentage字段中的百分比并计算总数。

$(document).on('change keyup blur', '.add', function(e) {
  add();
});

function add() {
  salary = $('#salary').val();
  allowance = $('#allowance').val() || 0;
  over = $('#over').val() || 0;
  prevTotal = $('#total').val() || 9000;

  if (allowance > 0 || over > 0) {
    total = parseFloat(salary) + parseFloat(allowance) + parseFloat(over);
    $('#total').val(total);
  } else {
    $('#total').val(prevTotal);
  }
}

$(document).on('change keyup blur', '.tax', function(e) {
  subtracttax();
});

function subtracttax() {
  salary = $('#salary').val();
  taxPercentage = $('#taxPercentage').val() || 0;
  tax = $('#tax').val() || 0;
  prevTotal = $('#total').val() || 9000;

  if (taxPercentage > 0) {
    total = parseFloat(salary) * parseFloat(taxPercentage) / 100;
    $('#tax').val(total);
    $('#total').val(prevTotal - total);
  } else {
    $('#total').val(prevTotal);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div>
  <label> salary </label>
  <input type="text" id="salary" value="9000" />
</div>
<br>
<div>
  <label> over Time </label>
  <input type="text" class="add" id="over" />
</div>
<br>
<div>
  <label> allowance </label>
  <input type="text" class="add" id="allowance" />
</div>
<br>
<div>
  <label> Tax Percentage</label>
  <input type="text" class="tax" id="taxPercentage" />
</div>
<br>
<div>
  <label> Total Tax</label>
  <input type="text" class="tax" id="tax" />
</div>
<br>
<div>
  <label> Total salary</label>
  <input type="text" id="total" />
</div>

2 个答案:

答案 0 :(得分:1)

尝试这样。

根据您的comment,我只会随着时间的推移加薪+。

calculateTax();
$(document).on('change keyup blur', '.tax', function(e) {
  calculateTax();
});

$(document).on('change keyup blur', '.taxVal', function(e) {
  calculateTaxPer();
});

function calculateTax() {
  salary = $('#salary').val();
  allowance = $('#allowance').val() || 0;
  over = $('#over').val() || 0;
  total = (parseFloat(salary) + parseFloat(allowance) + parseFloat(over)) || 0;
  taxPercentage = $('#taxPercentage').val() || 0;
  tax = $('#tax').val() || 0;
  $('#total').val(total);
  
  if (taxPercentage > 0) {
    totalTax = (parseFloat(salary) + parseFloat(over)) * parseFloat(taxPercentage) / 100;
    $('#tax').val(totalTax);
    $('#total').val(total - totalTax);
    return false;
  }
  
}

function calculateTaxPer(){
  salary = $('#salary').val();
  allowance = $('#allowance').val() || 0;
  over = $('#over').val() || 0;
  total = (parseFloat(salary) + parseFloat(allowance) + parseFloat(over)) || 0;
  taxPercentage = $('#taxPercentage').val() || 0;
  tax = $('#tax').val() || 0;
  $('#total').val(total);
  
  if(tax > 0){
    taxPer = (parseFloat(tax)/(parseFloat(salary) + parseFloat(over)))*100;
    $('#taxPercentage').val(taxPer);
    $('#total').val(total - tax);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div>
  <label> salary </label>
  <input type="text" id="salary" value="9000" />
</div>
<br>
<div>
  <label> over Time </label>
  <input type="text" class="tax" id="over" />
</div>
<br>
<div>
  <label> allowance </label>
  <input type="text" class="tax" id="allowance" />
</div>
<br>
<div>
  <label> Tax Percentage</label>
  <input type="text" class="tax" id="taxPercentage" />
</div>
<br>
<div>
  <label> Total Tax</label>
  <input type="text" class="taxVal" id="tax" />
</div>
<br>
<div>
  <label> Total salary</label>
  <input type="text" id="total" />
</div>

答案 1 :(得分:0)

在减去税函数中更改此行

prevTotal = $('#total').val() || 9000;

prevTotal = parseFloat(salary) + parseFloat(allowance) + parseFloat(over);

问题是输入税值后您是否要覆盖总值。相反,您应该在每次进行更改时计算总计。