一点计算

时间:2017-10-23 18:56:59

标签: javascript jquery

使用此代码,如何将乘法*设为#ttc = #totalcout * #marge

当我点击复选框时,我设法添加,但我无法将乘法集成到TTC中

// Addition when click checkbox
$("input[type=checkbox]").click(function(e) {
  var total = 0;
  $("input[type=checkbox]:checked").each(function() {
    total += parseFloat($(this).val());
  });
  if (total == 0) {
    $("#totalcout").html(0);
  } else {
    $("#totalcout").html(total + ' €');
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered">
  <thead>
    <tr>
      <th>Coût HT</th>
      <th>Marge</th>
      <th>Prix TTC</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td align="center">
        <p id="totalcout"></p>
      </td>
      <td align="center">
        <p id="marge">5</p>
      </td>
      <td align="center">
        <p id="ttc"></p>
      </td>
    </tr>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

计算新总数后,多次marge次,并使用新计算的值更新#ttc

&#13;
&#13;
// Addition when click checkbox
$("input[type=checkbox]").click(function(e) {
    var total = 0;
    $("input[type=checkbox]:checked").each(function() {
        total += parseFloat($(this).val());
    });     
    if (total == 0) {
        $("#totalcout").html(0);
    } else {                
        $("#totalcout").html(total + ' €');
    }
    // Now that you've recalculated the total, multiply that times 'marge' and insert that into '#ttc'
    const marge = parseFloat($("#marge").html());
    $("#ttc").html(parseFloat(total) * marge + ' €');
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value=5>5</input>
<input type="checkbox" value=10>10</input>
<table class="table table-bordered">
  <thead>
    <tr>
      <th>Coût HT</th>
      <th>Marge</th>
      <th>Prix TTC</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td align="center">
        <p id="totalcout"></p>
      </td>
      <td align="center">
        <p id="marge">5</p>
      </td>
      <td align="center">
        <p id="ttc"></p>
      </td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;