简体   繁体   English

事件 jQuery 中目标 div(行)内的 Select 元素

[英]Select elements within target div (row) on event jQuery

I have a table from which I need to select all of the values of the row after an event done in the same row (in this case is a keyup event) with jQuery .我有一个表,我需要 select 使用jQuery在同一行中完成事件(在本例中是keyup事件)之后的所有行的值。 The table could have many rows but the selection has to be on the items of the row event.该表可能有很多行,但选择必须在行事件的项目上。

This is the HTML of the table:这是表的 HTML:

<table class="table" id="form-table">
    <thead>
        <tr>
            <th scope="col">#</th>
            <th scope="col">Description</th>
            <th scope="col">Qty</th>
            <th scope="col">Price</th>
            <th scope="col">Discount</th>
            <th scope="col">Amount</th>
            <th scope="col">Hidden</th>
        </tr>
    </thead>
    <tbody>
        <tr id="row-1">
            <th scope="row">1</th>
            <td class="description-column">
                <div class="form-group">
                    <select class="form-control" id="companySelect">
                        {% for product in product_options %}
                        <option>{{ product }}</option>
                        {% endfor %}
                    </select>
                </div>
            </td>
            <td class="qty-column">
                <div class="form-group">
                    <input type="number" class="form-control" id="qty" placeholder="Qty" min="0" step="1">
                </div>
            </td>
            <td class="price-column">
                <div class="form-group">
                    <input type="number" class="form-control" id="price" min="0.01" placeholder="Price">
                </div>
            </td>
            <td class="discount-column">
                <div class="form-group">
                    <input type="number" class="form-control" id="discount" placeholder="0" min="0.01" max="99.99">
                </div>
                <div>%</div>
            </td>
            <td class="amount-column"></td>
            <td class="hidden-column">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" value="" id="hiddenCheck">
                </div>
            </td>
        </tr>
    </tbody>
</table>

And looks like this on the site:在网站上看起来像这样:

网站中的表格

For more context: I am making a quote app in which I'll type the price and quantity of each item in each row and I need to update the "amount" column after each keyup .有关更多上下文:我正在制作一个报价应用程序,我将在其中输入每行中每个项目的价格和数量,并且我需要在每个keyup之后更新“金额”列。 So I need to know how to select the proper "qty", "price", "discount" and "amount" id's of the row typed to do that.所以我需要知道如何 select 正确的“数量”、“价格”、“折扣”和“金额”ID 的输入行来做到这一点。

Firstly, The id attribute is unique, So you should replace it with class like this首先, id属性是唯一的,所以你应该像这样用class替换它

<input type="number" class="form-control qty"  placeholder="Qty" min="0" step="1">

Secondly, You can get the tr by using .closest() like this $(this).closest("tr")其次,您可以像这样使用.closest()来获得tr $(this).closest("tr")

Finally, You can calculate individual row by creating another method named updateAmount_per_row like below:最后,您可以通过创建另一个名为updateAmount_per_row的方法来计算单个行,如下所示:

function updateAmount_per_row(tr){
  var qTy = getValue_by_className(tr, '.qty-column .qty');
  var price = getValue_by_className(tr, '.price-column .price');
  var amount = qTy * price;
  
  tr.find('.amount-column').html(amount);
}

 $(".qty-column, .price-column").on("keyup", function(){ var tr = $(this).closest("tr"); updateAmount_per_row(tr); }); function updateAmount_per_row(tr){ var qTy = getValue_by_className(tr, '.qty-column.qty'); var price = getValue_by_className(tr, '.price-column.price'); var amount = qTy * price; tr.find('.amount-column').html(amount); } function getValue_by_className(tr, className){ var value = parseInt(tr.find(className).val(), 10); return value >= 0? value: 1; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table" id="form-table"> <thead> <tr> <th scope="col">#</th> <th scope="col">Description</th> <th scope="col">Qty</th> <th scope="col">Price</th> <th scope="col">Discount</th> <th scope="col">Amount</th> <th scope="col">Hidden</th> </tr> </thead> <tbody> <tr id="row-1"> <th scope="row">1</th> <td class="description-column"> <div class="form-group"> <select class="form-control" class="companySelect"> <option>Product A</option> <option>Product B</option> </select> </div> </td> <td class="qty-column"> <div class="form-group"> <input type="number" class="form-control qty" placeholder="Qty" min="0" step="1"> </div> </td> <td class="price-column"> <div class="form-group"> <input type="number" class="form-control price" min="0.01" placeholder="Price"> </div> </td> <td class="discount-column"> <div class="form-group"> <input type="number" class="form-control" class="discount" placeholder="0" min="0.01" max="99.99"> </div> <div>%</div> </td> <td class="amount-column"></td> <td class="hidden-column"> <div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="hiddenCheck"> </div> </td> </tr> <tr id="row-2"> <th scope="row">2</th> <td class="description-column"> <div class="form-group"> <select class="form-control" class="companySelect"> <option>Product A</option> <option>Product B</option> </select> </div> </td> <td class="qty-column"> <div class="form-group"> <input type="number" class="form-control qty" placeholder="Qty" min="0" step="1"> </div> </td> <td class="price-column"> <div class="form-group"> <input type="number" class="form-control price" min="0.01" placeholder="Price"> </div> </td> <td class="discount-column"> <div class="form-group"> <input type="number" class="form-control" class="discount" placeholder="0" min="0.01" max="99.99"> </div> <div>%</div> </td> <td class="amount-column"></td> <td class="hidden-column"> <div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="hiddenCheck"> </div> </td> </tr> </tbody> </table>

Using Mai Phuong answer as testing I managed to make it work by using the target property instead of this , because this for some reason was referencing the whole document ( #document ).使用Mai Phuong答案作为测试,我设法通过使用target属性而不是this来使其工作,因为出于某种原因, this引用了整个文档( #document )。

Here is the keyup event listener:这是keyup事件监听器:

  $('.price, .qty, .discount').keyup((event) => {
    let tr = event.target.closest('tr');
    updateAmount(tr);
  });

and here's the final updateAmount function:这是最终的updateAmount function:

function updateAmount(tr) {
  let qty = $(tr).find('.qty').val();
  let price = $(tr).find('.price').val();
  let discount = $(tr).find('.discount').val();

  let amount = (qty * price * (100 - discount)) / 100;

  $(tr).find('.amount').html(amount);
}

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

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