使用JavaScript计算税款

时间:2018-10-18 12:29:57

标签: javascript html function dom tax

所以我的问题是如何为HTML输入添加功能calculate()以动态更改税款输入。例如,如果我在“收入”字段中输入10000,税应显示为3500,如果在财富字段中另外添加10000,则税应显示为5000。

HTML:

var income = document.getElementById('income').value;
var wealth = document.getElementById('wealth').value;
var tax = document.getElementById('tax');

function calculate() {
  tax.innerHTML = (0.35 * income) + (0.25 * wealth);
}
<div class="row">
  <div class="col1">
    <label for="income">Income:</label>
  </div>
  <div class="col2">
    <input type="number" name="income" min="0" id="income" oninput="calculate()" required><br>
  </div>
</div>

<div class="row">
  <div class="col1">
    <label for="wealth">Wealth:</label>
  </div>
  <div class="col2">
    <input type="number" name="wealth" min="0" id="wealth" oninput="calculate()" required><br>
  </div>
</div>

<div class="row">
  <div class="col1">
    <label for="tax">Tax:</label>
  </div>
  <div class="col2">
    <input type="number" name="tax" min="0" id="tax" disabled>
  </div>
</div>

3 个答案:

答案 0 :(得分:2)

var incomeEl = document.getElementById('income')
var wealthEl = document.getElementById('wealth')
var taxEl    = document.getElementById('tax');

function calculate() {  
  var incomeTax = 0.35 * incomeEl.value;
  var wealthTax = 0.25 * wealthEl.value;
  var tax =  incomeTax + wealthTax;

  // round with 2 decimal places
  taxEl.value = Math.round(tax * 100) / 100;
}

incomeEl.addEventListener('input', calculate);
wealthEl.addEventListener('input', calculate);
<div class="row">
  <div class="col1">
    <label for="income">Income:</label>
  </div>
  <div class="col2">
    <input type="number" name="income" min="0" id="income" required><br>
  </div>
</div>

<div class="row">
  <div class="col1">
    <label for="wealth">Wealth:</label>
  </div>
  <div class="col2">
    <input type="number" name="wealth" min="0" id="wealth" required><br>
  </div>
</div>

<div class="row">
  <div class="col1">
    <label for="tax">Tax:</label>
  </div>
  <div class="col2">
    <input type="number" name="tax" min="0" id="tax" disabled>
  </div>
</div>

答案 1 :(得分:0)

您需要使用tax.value而不是tax.innerHTML,因为输入需要在其中输入一个值。然后,您只需将数字放置在函数中即可“刷新”包含数字的变量(该变量在字段的每个输入更改上运行,因此变量将刷新自身),如下所示:

var tax = document.getElementById('tax');

function calculate() {
  var income = document.getElementById('income').value;
  var wealth = document.getElementById('wealth').value;
  tax.value = (0.35 * income) + (0.25 * wealth);
}
<div class="row">
  <div class="col1">
    <label for="income">Income:</label>
  </div>
  <div class="col2">
    <input type="number" name="income" min="0" id="income" oninput="calculate()" required><br>
  </div>
</div>

<div class="row">
  <div class="col1">
    <label for="wealth">Wealth:</label>
  </div>
  <div class="col2">
    <input type="number" name="wealth" min="0" id="wealth" oninput="calculate()" required><br>
  </div>
</div>

<div class="row">
  <div class="col1">
    <label for="tax">Tax:</label>
  </div>
  <div class="col2">
    <input type="number" name="tax" min="0" id="tax" disabled>
  </div>
</div>

答案 2 :(得分:0)

  

您可以使用jquery

2013-01-07