Sum the cell from another cell in html using jquery and show result in another cell

huangapple go评论59阅读模式
英文:

Sum the cell from another cell in html using jquery and show result in another cell

问题

我想对同一行的另一个单元格中的特定单元格进行求和,并将结果粘贴到该行的总列中。

以下是我的代码。

alert($(this).find('#table tr td input#quantity').val());

但它不起作用,弹出警告时显示未定义/非数字错误。

英文:

I want to sum focused cell in a table from another cell of same row and paste result in a total column of that row.

Below is my code.

> alert($(this).find('#table tr td input#quantity').val());

But its not working as giving me Undefined/Nan Error in alert.

Sum the cell from another cell in html using jquery and show result in another cell

答案1

得分: 2

我建议使用CSS类而不是ID,这样可以计算单独的行并具有任意数量的行。还提供了总和的示例。

input{
  max-width:100px;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="table">
  <tr>
    <td><input></td>
    <td><input></td>
    <td><input></td>
    <td><input></td>
    <td><input readonly class="sum" placeholder="Sum"></td>
  </tr>
  <tr>
    <td><input></td>
    <td><input></td>
    <td><input></td>
    <td><input></td>
    <td><input readonly class="sum" placeholder="Sum"></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><input readonly class="total" placeholder="Total"></td>
  </tr>
</table>
英文:

I recommend to use CSS classes instead of IDs that will allow you to calculate a separate row and have any numbers of rows. An example of the total sum is also provided.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#table&#39;).on(&#39;input&#39;, &#39;input:not(.sum, .total)&#39;, e =&gt; {

  const $table = $(e.originalEvent.currentTarget);
  const $row = $(e.target).closest(&#39;tr&#39;);
  
  const calcSum = (elem, from) =&gt; {
    const sum = [...from].reduce((sum, elem) =&gt; {
      const val = parseFloat(elem.value);
      return sum + (val === val ? val : 0);
    }, 0) || NaN;
    elem.val(sum === sum ? sum : &#39;&#39;);
  };
  
  // calc sum of the row
  calcSum($row.find(&#39;.sum&#39;), $row.find(&#39;:not(.sum)&#39;));
 
  // calc total
  calcSum($table.find(&#39;.total&#39;), $table.find(&#39;.sum&#39;));
 
});

<!-- language: lang-css -->

input{
  max-width:100px;
  text-align: center;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table id=&quot;table&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input readonly class=&quot;sum&quot; placeholder=&quot;Sum&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input readonly class=&quot;sum&quot; placeholder=&quot;Sum&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input readonly class=&quot;total&quot; placeholder=&quot;Total&quot;&gt;&lt;/td&gt;
  &lt;/tr&gt;  
&lt;/table&gt;

<!-- end snippet -->

答案2

得分: 2

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<body>

<!DOCTYPE html>
<html>
<head>
  <title> Example</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
  <table id="table">
    <thead>
      <tr>
        <th>Input 1</th>
        <th>Input 2</th>
        <th>Input 3</th>
        <th>Input 4</th>
        <th>Input 5</th>
        <th>Total</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="text" class="input-box" /></td>
        <td><input type="text" class="input-box" /></td>
        <td><input type="text" class="input-box" /></td>
        <td><input type="text" class="input-box" /></td>
        <td><input type="text" class="input-box" /></td>
        <td><input type="text" class="total-input" readonly /></td>
      </tr>
      <!-- Add more rows as needed -->
    </tbody>
  </table>

  <script src="script.js"></script>
</body>
</html>


<script>
$(document).ready(function() {
  // Function to update the total
  function updateTotal(row) {
    var total = 0;
    var inputs = row.find('.input-box');

    // Iterate through each input box in the current row
    inputs.each(function() {
      var value = parseFloat($(this).val());

      // Check if the value is a valid number
      if (!isNaN(value)) {
        total += value;
      }
    });

    // Set the total value in the last input box of the same row
    row.find('.total-input').val(total);
  }

  // Listen for input changes on the input boxes
  $('.input-box').on('input', function() {
    var row = $(this).closest('tr');
    updateTotal(row);
  });
});

</script>

</body>
</html>

<!-- end snippet -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Example&lt;/title&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table id=&quot;table&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Input 1&lt;/th&gt;
&lt;th&gt;Input 2&lt;/th&gt;
&lt;th&gt;Input 3&lt;/th&gt;
&lt;th&gt;Input 4&lt;/th&gt;
&lt;th&gt;Input 5&lt;/th&gt;
&lt;th&gt;Total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;input-box&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;input-box&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;input-box&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;input-box&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;input-box&quot; /&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;total-input&quot; readonly /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;!-- Add more rows as needed --&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;script&gt;
$(document).ready(function() {
// Function to update the total
function updateTotal(row) {
var total = 0;
var inputs = row.find(&#39;.input-box&#39;);
// Iterate through each input box in the current row
inputs.each(function() {
var value = parseFloat($(this).val());
// Check if the value is a valid number
if (!isNaN(value)) {
total += value;
}
});
// Set the total value in the last input box of the same row
row.find(&#39;.total-input&#39;).val(total);
}
// Listen for input changes on the input boxes
$(&#39;.input-box&#39;).on(&#39;input&#39;, function() {
var row = $(this).closest(&#39;tr&#39;);
updateTotal(row);
});
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 19:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定