我正在尝试对“运行总计”进行一些计算,这是我的代码:
$('.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 是我想要更新该项目的总成本的位置,即。商品 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();
});
最佳答案
您需要找到.cost_of_items
在 <tr>
包含this
:
$(this).closest('tr').find('.cost_of_items')
关于jquery - 在 jQuery 的 .closest() 中使用类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4096950/