根据每种税种的税名添加税额

时间:2016-01-21 05:45:47

标签: jquery

我创建了一个类似于以下格式的发票页面

Item name desc discount unitprice tax1 tax2 price

在下面有

 subTotal   540
   Vat @5%  550
   Tax2@12% 430
   Tax3@13% 543
    ....
    total   

此处每件商品都有我想要显示的最多两种税 根据税名和每个项目征税,然后最后总计所有税款总和

对于税1我使用:

<span id="item_tax1">
   <input type="text" name="tax1name" value="VAT" class="tax1name tax input-mini"/> 
   <input type="hidden" name="tax1value" value="5" class="tax1value input-mini"/>
   <input type="hidden" name="tax1"  value="44" class="tax1 input-mini"/> 
</span>

对于税收2我使用:

<span id="item_tax2">
   <input type="text" name="tax1name" value="Service" class="tax2name tax input-mini"/> 
   <input type="hidden" name="tax2value" value="4" cssClass="tax2value input-mini"/>
   <input type="hidden" name="tax2"  value="45" class="tax2 input-mini"/> 
</span>

我正在计算税1和税2的税,并将其存储到<input type="hidden" name="tax1" class="tax1 input-mini"/>和税收2 class="tax2"

现在我想根据税名添加所有税费,如上所示。 怎么做到这一点?

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var taxes = {},
    total = 0;

$('.tax').each(function(){
  var elm = $(this),
      name = elm.val(),
      amount = elm.next().val();

  if (!taxes[name]) {
    taxes[name] = 0;
  }
  taxes[name] += parseFloat(amount);
  total += parseFloat(amount)
});

for (var tax in taxes) {
  $('<div />').html(tax + ': ' + taxes[tax]).appendTo('#result');
}

$('#result').append('total: ' + total);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      VAT1
    </td>
    <td>
      VAT2
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" name="tax1name" value="VAT" class="tax1name tax input-mini"/> 
      <input type="text" name="VAT1" value="10" cssClass="tax1value input-mini"/> 
    </td>
    <td>
      <input type="text" name="tax2name" value="serviceTax" class="tax1name tax input-mini"/> 
      <input type="text" name="VAT2" value="23" cssClass="tax2value input-mini"/> 
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" name="tax1name" value="education" class="tax1name tax input-mini"/> 
      <input type="text" name="VAT1" value="42" cssClass="tax1value input-mini"/> 
    </td>
    <td>
      <input type="text" name="tax2name" value="VAT" class="tax1name tax input-mini"/> 
      <input type="text" name="VAT2" value="55" cssClass="tax2value input-mini"/> 
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" name="tax1name" value="serviceTax" class="tax1name tax input-mini"/> 
      <input type="text" name="VAT1" value="68" cssClass="tax1value input-mini"/> 
    </td>
    <td>
      <input type="text" name="tax2name" value="education" class="tax1name tax input-mini"/> 
      <input type="text" name="VAT2" value="66" cssClass="tax2value input-mini"/> 
    </td>
  </tr>
</table>

<div id="result"></div>
&#13;
&#13;
&#13;