如何解决js求和1行和一列? JS

时间:2018-05-09 05:22:13

标签: javascript sum frontend

    <table class="table table-bordered">
      <thead class="thead-dark">
        <tr>
          <th scope="col">Description</th>
          <th scope="col">Cubic Meter</th>
          <th scope="col">Rate</th>
          <th scope="col">Taka</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td scope="row">Bortoman Reading</td>
          <td><input type="number" min="0" name="cm" placeholder="xx"></td>
          <td><input type="number" min="0" name="rate" placeholder="xx"></td>
          <td><input type="number" min="0" name="tk" placeholder="TK"></td>
        </tr>
        <tr>
          <td scope="row">Purber Reading</td>
          <td><input type="number" min="0" name="cm" placeholder="xx"></td>
          <td><input type="number" min="0" name="rate" placeholder="xx"></td>
          <td><input type="number" min="0" name="tk" placeholder="TK"></td>
        </tr>
<tr>
          <td scope="row" colspan="2">15% vat</td>
          <td><input type="number" min="0" name="rate" placeholder="xx"></td>
          <td><input type="number" min="0" name="tk" placeholder="TK"></td>
        </tr>
        <tr>
          <td scope="row">Total</td>
          <td><input type="number" min="0" name="TOTAL CUBIC METER" placeholder="xx"></td>
          <td><input type="number" min="0" name="TOTAL RATE" placeholder="xx"></td>
          <td><input type="number" min="0" name="TOTAL TAKA" placeholder="TK"></td>
        </tr>
      </tbody>
    </table>

我需要在js中解决这个问题。我想将行“CM + RATE = TK”和列“ALL CM ROW(总计立方米中的总和)+所有速率行(总速率求和)=所有Tk ROW(总TAKA中的求和)”相加。我想你得到了我想做的事。你能告诉我怎样才能解决这个问题。我需要通过此代码的确切示例。这对我更有帮助。并且用户也可以给空字段它必须使用空字段。 15%的增值税字段可以为空或不可以。

2 个答案:

答案 0 :(得分:0)

试试此代码

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table class="table table-bordered">
  <thead class="thead-dark">
    <tr>
      <th scope="col">Description</th>
      <th scope="col">Cubic Meter</th>
      <th scope="col">Rate</th>
      <th scope="col">Taka</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Bortoman Reading</th>
      <td><input type="number" min="0" name="cm" placeholder="xx"></td>
      <td><input type="number" min="0" name="rate" placeholder="xx"></td>
      <td><input type="number" min="0" name="tk" placeholder="TK"></td>
    </tr>
    <tr>
      <th scope="row">Purber Reading</th>
      <td><input type="number" min="0" name="cm" placeholder="xx"></td>
      <td><input type="number" min="0" name="rate" placeholder="xx"></td>
      <td><input type="number" min="0" name="tk" placeholder="TK"></td>
    </tr>
    <tr>
      <th scope="row">Total</th>
      <td><input type="number" min="0" name="TOTAL CUBIC METER" placeholder="xx"></td>
      <td><input type="number" min="0" name="TOTAL RATE" placeholder="xx"></td>
      <td><input type="number" min="0" name="TOTAL TAKA" placeholder="TK"></td>
    </tr>
  </tbody>
</table>
&#13;
$("tr").each(function() {
  if ($(this).children("td").length) {
    $($($(this).children("td")[3]).children("input")[0]).val(

      (($($($(this).children("td")[2]).children("input")[0]).val()) ? Number($($($(this).children("td")[2]).children("input")[0]).val()) : 0) +
      (($($($(this).children("td")[1]).children("input")[0]).val()) ? Number($($($(this).children("td")[1]).children("input")[0]).val()) : 0)
    )
  }
});
&#13;
&#13;
&#13;

对于codepen中的代码,请执行此操作

function a () { return 1 }

x = a;
y = a();

答案 1 :(得分:0)

总计的附加计算,它将对每一行的所有立方米和费率求和,并将值输入到最后一行。

 document.getElementById('calculate').addEventListener('click', function() {
   var tr = document.querySelectorAll('table tr');
   var td = document.querySelectorAll('table tr:last-child td > input');
   var totalcm = 0,
     totalrate = 0;
   tr.forEach(function(el, i) {
     if (i !== 0) {
       let cm = el.querySelectorAll('td > input')[0].value;
       let rate = el.querySelectorAll('td > input')[1].value;
       el.querySelectorAll('td > input')[2].value = Number(cm) + Number(rate);
       totalcm += Number(cm);
       totalrate += Number(rate);

     }
   })
   td[0].value = totalcm;
   td[1].value = totalrate;
   td[2].value = totalrate + totalcm;
 });

您必须在HTML中添加:

<button type="button" id="calculate">Calculate</button>