【发布时间】:2021-04-30 20:16:07
【问题描述】:
我有一个表,在使用jQuery 在同一行中完成事件(在本例中为keyup 事件)之后,我需要从中选择该行的所有值。表格可以有很多行,但选择必须在行事件的项目上。
这是表格的 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>
在网站上看起来像这样:
有关更多上下文:我正在制作一个报价应用程序,我将在其中输入每行中每个项目的价格和数量,并且我需要在每个 keyup 之后更新“金额”列。所以我需要知道如何为输入的行选择正确的“数量”、“价格”、“折扣”和“金额”ID。
【问题讨论】:
-
$(this).closest("tr")? -
您不应在每一行中重复 ID。改用类。
$(this).closest("tr").find(".qty") -
@Barmar OP 正在使用
id,所以我们无法通过类名<input type="number" class="form-control" id="qty"找到它 -
@MaiPhuong 我知道了,我让他去上课了。
标签: javascript jquery jquery-selectors