如何使用标签中的值并进行计算

时间:2019-06-04 08:12:44

标签: javascript

我有一个无法解决的问题。谁能帮忙。我有一个selectbox增值税,用户可以在其中选择oui或non。如果是,我必须对总价值征收15%的税。

从表格中获取值:

    <tr>
        <td class="centrer bold">Sous-total</td><td></td>
        <td><label id="soustotal" class="soustotal"><?php echo (number_format($sousTotal,2)); ?></label></td>
    </tr>
    <tr>
        <td class="centrer bold">VAT</td>
        <td>
            <select id="cmbvat" name="cmbvat" class="vat" style="width:100px" onchange="updateInput()">
                <option value="0">Non</option>
                <option value="15">Oui</option>
            </select> 
        </td>
        <td><label class="vat"><?php echo (number_format($VAT,2)); ?></label></td>
    </tr>
    <tr>
        <td class="centrer bold">Total TTC</td><td></td>
        <td><label class="grandtotal"><?php echo (number_format(($sousTotal - $VAT),2)); ?></label></td>
    </tr>

这是JS代码:

function updateInput()
{
    var vat = parseInt(document.getElementsByName("cmbvat")[0].value);
    var soustotal = parseFloat(document.getElementById("soustotal").innerHTML);

    var vatvalue = (vat * soustotal);

    alert(vatvalue);

}

alert(vatvalue)返回15? 警报(总数)应为1,910.00时返回1;

如果没有parseFlaot,我得到1,910.00:soustotal = document.getElementById(“ soustotal”)。innerHTML;

仅当我将parseFloat传递给soustotal时,我才获得值1而不是1,910.00 ...

2 个答案:

答案 0 :(得分:1)

function updateInput() {
  if (document.getElementById("qui").selected) {
    var soustotal = Number(document.getElementById("soustotal").innerHTML);
    var tax = (10 * parseFloat(soustotal) / 100);
    document.getElementById("vat").innerHTML = tax;
    document.getElementById("grandtotal").innerHTML = tax + soustotal;
  } else {
    document.getElementById("vat").innerHTML = "";
    document.getElementById("grandtotal").innerHTML = "";
  }
}
<html>

<head>
  <title>VAT</title>
  <script src="Sjabloon.js"></script>
</head>

<body>
  <table style="width:50%">
    <tr>
      <td class="centrer bold">Sous-total</td>
      <td></td>
      <td><label id="soustotal" class="soustotal">5</label></td>
    </tr>
    <tr>
      <td class="centrer bold">VAT</td>
      <td>
        <select id="cmbvat" name="cmbvat" class="vat" style="width:100px" onchange="updateInput()">
          <option value="0" id="non">Non</option>
          <option value="15" id="qui">Oui</option>
        </select>
      </td>
      <td><label class="vat" id="vat"></label></td>
    </tr>
    <tr>
      <td class="centrer bold">Total TTC</td>
      <td></td>
      <td><label class="grandtotal" id="grandtotal"></label></td>
    </tr>
  </table>
</body>

</html>

这对我来说很好。至少如果这是您想要的?

希望这会有所帮助。

答案 1 :(得分:0)

https://jsfiddle.net/uv786wy2/

var soustotal = parseFloat(document.getElementById("soustotal").innerHTML);
var tax = ((10 * (soustotal))/100);
document.getElementById('tax').textContent = tax;