【发布时间】:2020-04-13 15:24:20
【问题描述】:
我有一个购物车,其中包含每个产品的价格、数量和小计字段。数量字段可由用户更改,但其他字段是静态的。
有没有办法在用户更改数量时计算小计?我想将数量乘以价格,然后更新小计,而不需要用户提交表单。此外,在计算小计时,也应更新总计。
我的代码是一个 Django 模板,看起来像这样,相关部分在循环中:
<div class="container">
<table id="cart" class="table table-hover table-condensed">
<thead>
<tr>
<th style="width:50%">Product</th>
<th style="width:10%">Price</th>
<th style="width:8%">Quantity</th>
<th style="width:22%" class="text-center">Subtotal</th>
</tr>
</thead>
{% for crops_ordered_names,crops_ordered_images,crops_ordered_cost,crops_ava in total %}
<tbody>
<tr>
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"><img src="http://placehold.it/100x100" alt="..." class="img-responsive"/></div>
<div class="col-sm-10">
<h4 class="nomargin">{{crops_ordered_names}}</h4>
<p>Available amount: {{crops_ava}}</p>
</div>
</div>
</td>
<td data-th="Price" id="price">{{crops_ordered_cost}}</td>
<td data-th="Quantity">
<input type="number" class="form-control text-center" id="quan" min="1" max="{{crops_ava}}">
</td>
<td data-th="Subtotal" class="text-center" id="subtotal"></td>
<td class="actions" data-th="">
<button class="btn btn-danger btn-sm"><i class="fa fa-trash-o"></i></button>
</td>
</tr>
</tbody>
{% endfor %}
<tfoot>
<tr class="visible-xs">
<td class="text-center"><strong>Total 1.99</strong></td>
</tr>
<tr>
<td colspan="2" class="hidden-xs"></td>
<td class="hidden-xs text-center" id="total"><strong>Total </strong></td>
</tr>
</tfoot>
</table>
【问题讨论】:
标签: javascript jquery html django forms