0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Table内のTextのイベントで同一行の値操作

Last updated at Posted at 2021-07-12

タイトルの通り
Table内のテキストの入力値を別のTd要素の値との差分をさらに別のTd要素に表示するというもの
取るに足らない内容ですが、やり方調べるのに少し時間がかかってしまったので記録用に残しておきます。

table.html

<table>
  <thead>
    <tr id="head">
      <th>XXX</th>
      <th style="width: 100px;">在庫</th>
      <th style="width: 100px;">使用量</th>
      <th style="width: 100px;">残数</th>
    </tr>
  </thead>
  <colgroup>
    <col style="width: 15%;" />
    <col style="width: 8%;" />
    <col style="width: 8%;" />
    <col style="width: 8%;" />
  </colgroup>
  <tbody>
    <tr id="tr1">
      <th>部品1</th>
      <td>1,500</td>
      <td><input name='textOnTd' type="text" value="1,000"></td>
      <td class="txt_right">0</td>
    </tr>
    <tr id="tr2">
      <th>部品2</th>
      <td>3,000</td>
      <td><input name='textOnTd' type="text" value="1,000"></td>
      <td class="txt_right">0</td>
    </tr>
  </tbody>
</table>

table.js

  $(function () {
    $("[name^=textOnTd]").on("keydown", function () {
      var use = $(this).closest('tr').find('input[name=textOnTd]').val().replace(/,/g, '');
      var stock = $(this).closest('tr').children("td")[0].innerText.replace(/,/g, '');
      var sum = Number(stock) - Number(use);
      $(this).closest('tr').children("td")[2].innerText = sum.toLocaleString();;
    });
  });

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?