根据jquery

时间:2017-01-16 15:35:19

标签: jquery

我正试图从输入中得到平均值。

我有5个输入字段。我想做这样的事情:

n = total of inputed field (has value)
total = n1 + n2 + ... + n5 / n

例如:

1 + 2 + 3 / 3
1 + 2 + 3 + 4 / 4
1 + 2 + 3 + 4 + 5 / 5

平均值取决于用户输入文本字段的数量。这是我到目前为止所尝试的。请检查一下。

我的脚本如下。我只能总结,我不能做average

$(document).ready(function() {
  $(document).on("change", ".kd1", function() {
    $('table tr').each(function() {
      var valid_labels = 0;
      var newval = 0;
      var total = $('.kd1', this).get().reduce(function(sum, elem) {
        return sum + +$(elem).val();
      }, 0)
      if (!isNaN(total)) {
        valid_labels += 1;
        newval += total;
      }
      $('.result1', this).val(newval / valid_labels);
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
  <tr>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='result1'></td>
  </tr>
  <tr>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='kd1'></td>
    <td><input type='text' size='5' class='result1'></td>
  </tr>
</table>
对不起,我的英语不好。

参考:https://stackoverflow.com/a/14899614/6354277

1 个答案:

答案 0 :(得分:2)

迭代输入字段,添加非空的值以及递增计数。

&#13;
&#13;
$(document).ready(function() {
  $(document).on("input", ".kd1", function() {
    $('table tr').each(function() {
      // variables for holding total and count
      var total = 0,
        count = 0;

      // get all input fields and iterate over them
      $('.kd1', this).each(function() {
        // check the value is non-empty
        if (this.value.trim() != '') {
          // increment count for calculating average
          count++;
          // update total based on input value
          // treat input value as 0 if number parsing produces NaN
          total += (Number(this.value.trim()) || 0);
        }
      });
      // calculate and update the average although treat as zero if NaN
      $('.result1', this).val(total / count || 0);
    });
  });

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
  <tr>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='result1'>
    </td>
  </tr>
  <tr>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='kd1'>
    </td>
    <td>
      <input type='text' size='5' class='result1'>
    </td>
  </tr>
</table>
&#13;
&#13;
&#13;