从填充的字段中减去两个值

时间:2018-03-26 16:00:19

标签: javascript

我有一个表格,其中包含一些人将填写的输入字段。这是一个应用程序。我有每组表的总数。现在,这些正在将各个字段的总数相加到每个表的总框中。我需要一个额外的字段来减去这两个字段,给我一个广泛的总和。由于某种原因,我没有控制台错误或总输出。

UNWIND ids AS id
MATCH (e:Element)-[r]->(f:Foo {id: id})
WITH e, COUNT(r) AS num WHERE num = SIZE(ids)
RETURN e
function findTotal() {
  var arr = document.getElementsByClassName('qty');
  var tot = 0;
  for (var i = 0; i < arr.length; i++) {
    if (parseInt(arr[i].value))
      tot += parseInt(arr[i].value);
  }
  document.getElementById('total_Assets').value = tot;
}

function findTotal2() {
  var arr = document.getElementsByClassName('qty2');
  var tot = 0;
  for (var i = 0; i < arr.length; i++) {
    if (parseInt(arr[i].value))
      tot += parseInt(arr[i].value);
  }
  document.getElementById('total_liabilities').value = tot;
}

function fnCalculate() {
  //Get the texts from both textboxes
  var txt1 = document.getElementById("total_Assets").value;
  var txt2 = document.getElementById("total_liabilities").value;
  //Substract that
  var res = parseInt(txt1) - parseInt(txt2);
  //Set the value to your div
  document.getElementById("net_worth").innerHtml = res;
}

1 个答案:

答案 0 :(得分:1)

我解决了两个问题:

  1. 我已将.innerHtml替换为.value属性
  2. 每当条目失去焦点时,我会调用所有计算。我认为计算没有被正确触发
  3. 除此之外,我还用value替换placeholder属性的使用来显示字段名称。这样,您无需在输入任何内容之前删除文本。

    &#13;
    &#13;
    function findTotal() {
      var arr = document.getElementsByClassName('qty');
      var tot = 0;
      for (var i = 0; i < arr.length; i++) {
        if (parseInt(arr[i].value))
          tot += parseInt(arr[i].value);
      }
      document.getElementById('total_Assets').value = tot;
    }
    
    function findTotal2() {
      var arr = document.getElementsByClassName('qty2');
      var tot = 0;
      for (var i = 0; i < arr.length; i++) {
        if (parseInt(arr[i].value))
          tot += parseInt(arr[i].value);
      }
      document.getElementById('total_liabilities').value = tot;
    }
    
    function fnCalculate() {
      findTotal();
      findTotal2();
      //Get the texts from both textboxes
      var txt1 = document.getElementById("total_Assets").value;
      var txt2 = document.getElementById("total_liabilities").value;
      //Substract that
      var res = parseInt(txt1) - parseInt(txt2);
      //Set the value to your div
      document.getElementById("net_worth").value= res;
    }
    &#13;
    <table class="tg">
      <tr>
        <th class="tg-9hbo"><br></th>
        <th class="tg-9hbo">Amount ($)<br></th>
      </tr>
      <tr>
        <td class="tg-yw4l">Other Assets<br></td>
        <td class="tg-yw4l"><input class="qty" onblur="fnCalculate()" id="other_assets" type="text" name="other_assets" placeholder="[[+fi.other_assets]]" /></td>
      </tr>
      <tr>
        <td class="tg-yw4l"></td>
        <td class="tg-yw4l"><input class="qty" onblur="fnCalculate()" id="other_assets_2" type="text" name="other_assets_2" placeholder="[[+fi.other_assets_2]]" /></td>
      </tr>
      <tr>
        <td class="tg-yw4l">(A) TOTAL ASSETS<br></td>
        <td class="tg-yw4l"><input onblur="fnCalculate()" class="totals" id="total_Assets" type="text" name="total_Assets" placeholder="[[+fi.total_Assets]]" readonly /></td>
      </tr>
      <tr>
        <td class="tg-yw4l">(C) NET WORTH (A MINUS B)<br></td>
        <td class="tg-yw4l"><input id="net_worth" type="text" name="net_worth" placeholder="[[+fi.net_worth]]" readonly/></td>
    
      </tr>
    </table>
    <table class="tg">
      <tr>
        <th class="tg-9hbo"><br></th>
        <th class="tg-9hbo">Amount ($)<br></th>
      </tr>
      <tr>
        <td class="tg-yw4l">Bank Loans<br></td>
        <td class="tg-yw4l"><input class="qty2" onblur="fnCalculate()" id="bank_loans" type="text" name="bank_loans" placeholder="[[+fi.bank_loans]]" /></td>
      </tr>
      <tr>
        <td class="tg-yw4l">Mortgages and Real Estate<br></td>
        <td class="tg-yw4l"><input class="qty2" onblur="fnCalculate()" id="mortgages" type="text" name="mortgages" placeholder="[[+fi.mortgages]]" /></td>
      </tr>
      <tr>
        <td class="tg-yw4l">(B) TOTAL LIABILITIES<br></td>
        <td class="tg-yw4l"><input onblur="fnCalculate()" class="totals" id="total_liabilities" type="text" name="total_liabilities" placeholder="[[+fi.total_liabilities]]" readonly /></td>
      </tr>
    </table>
    &#13;
    &#13;
    &#13;