HTML表单javascript总和税额

时间:2015-06-19 17:23:59

标签: javascript html forms

我有一个非常好的工作形式,几乎一切都正常。但我需要计算和自动填充总税金。这是代码:

<table class="tableventa">
  <tr>
    <td style="width:200px;"></td>
    <td valign="top" style="width:900px;"><table class="table900">
      <tr>
        <td class="header150">Default unit price</td>
        <td class="header150">Type of sell</td>
        <td class="header150">Apply Taxes?</td>
        <td class="header150">Apply Discount?</td>
        <td class="header150">Quantity</td>
        </tr>
      <tr>
        <td class="header150small"><input type="text" class="input140" name="neto" id="neto" readonly value="1000"></td>
        <td class="header150small">

        <select class="select146" name="tipo" id="tipo">
  <option value="Simple" selected>Without Invoice</option>
  <option value="Boleta">Invoice 1</option>
   <option value="Factura">Invoice 2</option>
   </select>

   </td>
        <td class="header150small"><select class="select146" name="iva" id="iva">
  <option value="0" selected>Tax Free</option>
  <option value="19">YES 19%</option>

   </select></td>
        <td class="header150small">
         <select class="select146" name="descuento" id="descuento">
  <option value="0" selected>No Discount</option>
  <option value="2">2% Discount</option>
  <option value="3">3% Discount</option>
  <option value="5">5% Discount</option>
  <option value="8">8% Discount</option>
  <option value="10">10% Discount</option>
  <option value="15">15% Discount</option>
  <option value="20">20% Discount</option>
  <option value="30">30% Discount</option>
   <option value="50">50% Discount</option>

   </select>
   </td>
        <td class="header150small"><input type="text" class="input140" name="cantidad" id="cantidad" value="1"></td>
        </tr>
      <tr>
        <td class="header150">Price after discount</td>
        <td class="header150">Tax per unit</td>
        <td class="header150">Subtotal per unit</td>
        <td class="header150">Total Taxes</td>
        <td class="header150">Total to Pay</td>
        </tr>
      <tr>
        <td class="header150small"><input type="text" class="input140" name="preciodesc" id="preciodesc" readonly value="1000"></td>
   <td class="header150small"><input type="text" class="input140" name="ivaunitario" id="ivaunitario" readonly value="0"></td>
        <td class="header150small"><input type="text" class="input140" name="subtotal" id="subtotal" readonly value="1000"></td>
        <td class="header150small"><input type="text" class="input140" name="ivatotal" id="ivatotal" readonly value="0"></td>
        <td class="header150small"><input type="text" class="input140" name="total" id="total" readonly value="1000"></td>
        </tr>
      <tr>
        <td class="header150small">&nbsp;</td>
        <td class="header150small">&nbsp;</td>
        <td class="header150">Invlice Number</td>
        <td class="header150">&nbsp;</td>
        <td class="header150small">&nbsp;</td>
        </tr>
      <tr>
        <td class="header150small">&nbsp;</td>
        <td class="header150small">&nbsp;</td>
        <td class="header150small"><input type="text" class="input140" name="documento" id="documento" value=""></td>
        <td class="header150small"><input class="someter150" type="reset" value="Reset"></td>
        <td class="header150small"><input class="someter150" type="button" value="Sell now" onClick="document.productos.submit();"></td>
        </tr>
      </table></td>
  </tr>
</table>

<script>
var taxes    = document.getElementsByName('iva')[0];
var discount = document.getElementsByName('descuento')[0];
var cost     = document.getElementsByName('neto')[0];
var price    = document.getElementsByName('preciodesc')[0];
var ttaxes   = document.getElementsByName('ivaunitario')[0];
var total    = document.getElementsByName('subtotal')[0];
var quantity = document.getElementsByName('cantidad')[0];
var ttp      = document.getElementsByName('total')[0];

function updateInput() {
  price.value = cost.value - (cost.value * (discount.value / 100));
  ttaxes.value = (price.value * (taxes.value / 100));
  var sum = parseFloat(price.value) + parseFloat(ttaxes.value);
  total.value = sum.toFixed(0);
  ttp.value = sum.toFixed(0) * quantity.value;
}

taxes.addEventListener('change', updateInput);
discount.addEventListener('change', updateInput);
cost.addEventListener('change', updateInput);
cost.addEventListener('keyup', updateInput);
quantity.addEventListener('keyup', updateInput);

</script>
</form>

这是一个演示小提琴

https://fiddle.jshell.net/v6spxoqv/3/

这是我需要的图像

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为需要声明变量“Total Taxes”我认为它被称为“ivatotal”所以(它旁边的其他变量):

var ivatot   = document.getElementsByName('ivatotal')[0];

在“updateInput”函数中执行操作后(在已包含的操作结束时):

ivatot.value = parseFloat(ttaxes.value) * parseFloat(quantity.value);

功能“tipo_cambio”:

function tipo_cambio () {
var tipo = document.getElementsByName('tipo')[0];
if ( tipo.value == "Simple" )
     taxes.selectedIndex = 0;
else taxes.selectedIndex = 1;
updateInput();
}

必须从此选择中调用以前的函数:

<select class="select146" name="tipo" id="tipo" onchange="tipo_cambio()">

答案 1 :(得分:0)

您尚未在Javascript代码中设置“总计”字段的值。

ivatot.value = (ttaxes.value * quantity.value);