简体   繁体   English

使用 Javascript 或 JQuery 获取多个 ID

[英]Get IDs of Multiple <td> in a <tr> with Javascript or JQuery

I have the following in my html:我的 html 中有以下内容:

<table id = 'manifest-table25' class="manifest-table2" width=100%>
                  <tbody width=100%>
                    <tr class="manifest-row">
                      <td width = 17.5%>{{form2.ProductCode}}</td>
                      <td width = 32.5%>{{form2.DescriptionOfGoods}}</td>
                      <td width = 12.5% class="quantity">{{form2.UnitQty}}</td>
                      <td width = 12.5%>{{form2.Type}}</td>
                      <td width = 12.5% oninput="calculate()">{{form2.Price}}</td>
                      <td width = 12.5%>{{form2.Amount}}</td>
                    </tr>
                  </tbody>
                </table>

I have a function calculate() called oninput to the Price field, which multiplies UnitQty * Price and displays the result in Amount.我有一个 function calculate()调用 oninput 到 Price 字段,它将 UnitQty * Price 相乘并以 Amount 显示结果。

<script>

    function calculate() {
      var UnitQty = document.getElementById('id_form-0-UnitQty').value;
      var Price = document.getElementById('id_form-0-Price').value;
      var LineTotal = UnitQty * Price;
      $('#id_form-0-Amount').val(LineTotal);
}
</script>

This works perfectly, but is hardcoded to use those specific IDs.这完美地工作,但被硬编码以使用那些特定的 ID。 My dilemma is that the user has the ability to dynamically add rows to this table, and this will work only for one row.我的困境是用户可以动态地将行添加到该表中,而这仅适用于一行。 So if the user adds one more row, obviously the above is hardcoded and doesn't apply for that new row.因此,如果用户再添加一行,显然上述内容是硬编码的,不适用于该新行。 The function called for the new row would need to effectively look like this:为新行调用的 function 需要有效地看起来像这样:

<script>

    function calculate() {
      var UnitQty = document.getElementById('id_form-1-UnitQty').value;
      var Price = document.getElementById('id_form-1-Price').value;
      var LineTotal = UnitQty * Price;
      $('#id_form-1-Amount').val(LineTotal);
}
</script>

So I need some way to dynamically pick replace the IDs in that function, based on which is actually being typed in. I am completely stuck on thinking up an approach, can anyone please help with this?所以我需要一些方法来动态选择替换 function 中的 ID,基于它实际上正在输入。我完全坚持想一个方法,有人可以帮忙吗?

I'm going to guess that the {{...}} create input elements within those td elements.我猜想{{...}}在这些td元素中创建input元素。

A minimal change is to change最小的改变就是改变

oninput="calculate()"

to

oninput="calculate(this)"

which passes the element the event was fired by into the function, then use DOM navigation within the function:它将触发事件的元素传递到 function,然后在 function 中使用 DOM 导航:

function calculate(el) {
    // Find the row containing this cell
    var row = el.closest("tr");
    // Get the quantity from the `input` in the `.quantity` cell in this row
    var unitQty = row.querySelector('.quantity input').value;
    // Get the price from the `input` in this cell (could use `e.target` instead)
    var price = el.querySelector('input').value;
    // Do the calculation, assign to the `input` in the `.amount` cell in this row
    var lineTotal = unitQty * price;
    row.querySelector(".amount input").value = lineTotal;
}

(Note: I've updated that code to follow standard JavaScript variable naming conventions.) (注意:我已更新该代码以遵循标准 JavaScript 变量命名约定。)

To avoid making that really fragile, I've added a class to the final td for that last querySelector call.为了避免让它变得非常脆弱,我在最后一个querySelector调用的最终td中添加了一个 class 。

Or if you prefer jQuery:或者,如果您更喜欢 jQuery:

function calculate(el) {
    // Find the row containing this cell
    var row = $(el).closest("tr");
    // Get the quantity from the `input` in the `.quantity` cell in this row
    var unitQty = row.find('.quantity input').val();
    // Get the price from the `input` in this cell (could use `e.target` instead)
    var price = $(el).find('input').val();
    // Do the calculation, assign to the `input` in the `.amount` cell in this row
    var lineTotal = unitQty * price;
    row.find(".amount input").val(lineTotal);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM