【发布时间】:2020-12-21 19:01:32
【问题描述】:
这是我的动态表格输入表单。
<table id="calculate">
<tr>
<th>Price</th>
</tr>
<tbody data-role="dynamic-fields">
<tr class="form-inline">
<td>
<div class="input-group">
<input type="text" name="price[]" class="form-control">
</div>
</td>
<td>
<div class="input-group">
<button class="btn btn-primary btn-sm" data-role="add">
Add
</button>
</div>
</td>
</tr>
</tbody>
</table>
<input type="text" name="total_price" id="total_price">
这是当我点击按钮时在表格中追加新表单的 jquery
$(document).on(
'click',
'[data-role="dynamic-fields"] > .form-inline [data-role="add"]',
function(e) {
e.preventDefault();
var container = $(this).closest('[data-role="dynamic-fields"]');
new_field_group = container.children().filter('.form-inline:first-child').clone();
new_field_group.find('input').each(function(){
$(this).val('');
});
container.append(new_field_group);
}
);
现在我想通过点击添加按钮输入多个价格,所有价格都应该总计并附加到 total_price
$(document).on('change paste keyup', " input[name='price[]']", function(){
updatetotalprice($(this));
});
function updatetotalamount(selector) {
$parent = selector.parent('.input-group').parent('td').parent('tr'); [...$parent.querySelectorAll("tr")].forEach(row => {
$("[name=amount[]]").val()= total;
$("#total_price").val(total.toFixed(2));
})
}
【问题讨论】:
标签: jquery