在 jQuery 的 .closest() 中使用类名
我正在尝试对“运行总计”进行一些计算,这是我的代码:
$('.quantity_input').live('change',function(){
var ValOne = parseFloat($(this).val());
var ValTwo = parseFloat($(".price").text())
var totalTotal = ((ValOne) * (ValTwo));
$('.cost_of_items').closest('.cost_of_items').text(totalTotal.toFixed(2));
calcTotal();
});
.quantity_input 是输入,.price 是产品的价格,.cost_of_items 是我想要更新该项目的总成本的地方, IE。商品 1 = £5 x 3 数量 = 商品 1 的总计 £15 calcTotal() 是一个仅更新订单总成本的函数。问题是将所有数学保留在表的一行中,即我在上面的代码中进行计算,并且它不坚持其行,它用类 .cost_of_items 等更新所有字段...
显示的问题我的 html 是由 jQuery .appends() 动态添加的,但这里是相关的 jQuery:
$('#items').append('<tr class="tableRow"><td><a class="removeItem" href="#"><img src="/admin/images/delete.png"></img></a><td class="om_part_no">' + omPartNo + '</td><td>' + supPartNo + '</td><td>' + cat + '</td><td class="description">' + desc + '</td><td>' + manuf + '</td><td>' + list + '</td><td>' + disc + '</td><td><p class="add_edit">Add/Edit</p><input type="text" class="quantity_input" name="quantity_input" /></td><td class="price_each_nett price">' + priceEach + '</td><td class="cost_of_items"></td><td><p class="add_edit">Add/Edit</p><input type="text" class="project_ref_input" name="project_ref_input" /><p class="project_ref"></p></td></tr>');
编辑:
工作解决方案:
$('.quantity_input').live('change',function(){
var ValOne = parseFloat($(this).val());
var ValTwo = parseFloat($(this).closest('tr').find('.price').text())
var totalTotal = ((ValOne) * (ValTwo));
$(this).closest('tr').find('.cost_of_items').text(totalTotal.toFixed(2));
calcTotal();
});
I'm attempting to do some calculations for a "running total", this is my code:
$('.quantity_input').live('change',function(){
var ValOne = parseFloat($(this).val());
var ValTwo = parseFloat($(".price").text())
var totalTotal = ((ValOne) * (ValTwo));
$('.cost_of_items').closest('.cost_of_items').text(totalTotal.toFixed(2));
calcTotal();
});
.quantity_input is an input, .price is the price of the product, .cost_of_items is where i want to update the total cost for the item, ie. item1 = £5 x 3 quantity = £15 total for item1
calcTotal() is a function that just updates a total cost for the order. The problem is keeping all the math in one row of the table, ie i'm doing the calc in the code above and its not sticking to its row, its updating all the fields with class .cost_of_items etc...
the problem with showing my html is that its dynamically added by jQuery .appends() but here is the relevant jQuery:
$('#items').append('<tr class="tableRow"><td><a class="removeItem" href="#"><img src="/admin/images/delete.png"></img></a><td class="om_part_no">' + omPartNo + '</td><td>' + supPartNo + '</td><td>' + cat + '</td><td class="description">' + desc + '</td><td>' + manuf + '</td><td>' + list + '</td><td>' + disc + '</td><td><p class="add_edit">Add/Edit</p><input type="text" class="quantity_input" name="quantity_input" /></td><td class="price_each_nett price">' + priceEach + '</td><td class="cost_of_items"></td><td><p class="add_edit">Add/Edit</p><input type="text" class="project_ref_input" name="project_ref_input" /><p class="project_ref"></p></td></tr>');
EDIT:
Working solution:
$('.quantity_input').live('change',function(){
var ValOne = parseFloat($(this).val());
var ValTwo = parseFloat($(this).closest('tr').find('.price').text())
var totalTotal = ((ValOne) * (ValTwo));
$(this).closest('tr').find('.cost_of_items').text(totalTotal.toFixed(2));
calcTotal();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要在包含
this
的中找到
.cost_of_items
:You need to find the
.cost_of_items
in the<tr>
containingthis
:Closest 将找到最近的祖先(父母、祖父母),但是您将需要进行查找才能找到要更新的正确元素。例如,如果表行中有一个元素,并且同一行中需要另一个元素:
编辑:
在您的情况下,您需要
Closest will find the closest ancestor (parent, grandparent), but you will then need to do a find in order to find the correct element to update. For example, if you have an element in a table row and you need another element in that same row:
Edit:
In your case, you will want
我不会使用 .find() 。我猜测遍历到最近的
并使用
获取兄弟
类使用 jQuery 的可能会更有效。 cost_of_items
.siblings()
方法。编辑:为了澄清,这里是
.append()
的标记:I wouldn't use
.find()
. I'm guessing it will probably be a bit more efficient to traverse up to the closest<td>
and get the sibling<td>
with the.cost_of_items
class using jQuery's.siblings()
method.EDIT: To clarify, here's the markup from the
.append()
:您不需要使用查找。
closest
采用上下文参数。这可以帮助您缩小搜索范围。你应该使用它。You don't need to use find.
closest
takes a context argument. that helps you narrow down the field of search. You should use that.