我想取数量并乘以价格得到总数并将其打印到#total。并获取位于下面显示的部分项目表格之外的小计。
<table class='table table-striped table-hover table-bordered example'>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td id="item" class="col-md-3">
<%= f.association :items, collection: Item.all, label_method: :name, value_method: :id, placeholder: 'Item Name', input_html: { id: 'item-select2' } %>
</td>
<td id="price"></td>
<%= f.simple_fields_for :order_items do |o| %>
<td class="col-md-2"><%= o.input :quantity, input_html: { id: 'quantity' } %></td>
<% end %>
<td id="total" class="col-md-1"></td>
</tr>
</tbody>
</table>
$(document).ready(function() {
$("#client-select2").select2();
$("#item-select2").select2();
$("#item-select2").click(function() {
var selectedItem, total;
selectedItem = $(this);
if (selectedItem.val() !== null) {
$.ajax({
url: "/items.json",
data: { selectedItem: "[" + selectedItem.val() + "]" },
dataType: "json"
}).done(function(data) {
//console.log(data[1]);
var i, j;
i = 0;
j = 0;
while (i < selectedItem.val().length) {
while (j < data.length) {
if (String(selectedItem.val()[i]) === String(data[j].id)) {
$("#item").html(data[j].name);
$("#price").html(data[j].price);
total = parseFloat(data[j].price).toFixed(2);
}
++j;
}
++i;
}
$("#total").html(total);
return
}); // end of function(data)
} else {
$("#item").html("");
$("#price").html("");
$("#total").html("");
}
});
});