html - 如何添加动态值并将其附加到一个字段中?
问题描述
表单,它正在获取服务名称,<td>Name</td>
下面的 jQuery 将sub total
作为quantity
相对于price
.
$(document).on('change', '.qty', function() {
var quantity = $(this).val();
var sub = $(this).parent().next().children();
var price = $(this).parent().prev().children().val();
var subTotal = parseInt(quantity) * parseInt(price);
sub.val(subTotal);
});
var i = 1;
$(function() {
$("#btnAdd").bind("click", function() {
var div = $("<tr />");
div.html(GetDynamicTextBox(""));
$("#TextBoxContainer").append(div);
});
$("body").on("click", ".remove", function() {
$(this).closest("tr").remove();
// GrandTotal();
});
});
function GetDynamicTextBox(value) {
i++;
return '<td><select class="form-control select2 select-service" style="width: 100%;" id="service-select' + i + '" required name="product[]"><option value="">--Select Product--</option> <?php foreach($dbf->fetchOrder("service") as $services){?><option value="<?= $services['
id '];?>"><?= $services['
service '];?></option><?php }?></select></td>' +
'<td><input class="form-control rate" name = "rate[]" type="text" placeholder="Rate" id="rate1' + i + '" required readonly/></td>' +
'<td><input class="form-control qty" placeholder="Quantity" name = "qty[]" type="number" id="qty1' + i + '" /></td>' +
'<td><input class="form-control sub-total" placeholder="Sub Total" name = "sub_total[]" type="text" id="sub_total1' + i + '" readonly required/></td>' +
'<td><button type="button" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove-sign"></i></button></td>'
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-responsive table-striped table-bordered">
<thead>
<tr>
<td>Name</td>
<td>Price</td>
<td>Quantity</td>
<td>Sub Total</td>
<td><button id="btnAdd" type="button" class="btn btn-primary" data-toggle="tooltip" data-original-title="Add more controls"><i class="glyphicon glyphicon-plus-sign"></i> Add </button></td>
</tr>
</thead>
<tbody id="TextBoxContainer">
<tr>
<td>
<select class="form-control select-service" style="width: 100%;" id="service-select" required name="service[]">
<option value="">--Select Services--</option>
<?php foreach($dbf->fetchOrder("service") as $services){?>
<option value="<?= $services['id'];?>">
<?= $services['service'];?>
</option>
<?php }?>
</select>
</td>
<td><input type="text" class="form-control rate" placeholder="Rate" readonly name="rate[]" id="rate1" required></td>
<td><input type="number" min="1" class="form-control qty" placeholder="Quantity" name="qty[]" id="qty1"></td>
<td><input type="text" min="1" class="form-control sub_total" placeholder="Sub Total" name="sub_total[]" readonly id="sub_total1"></td>
<td><button type="button" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove-sign"></i></button></td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
这是上面同一个表单的多个动态输入字段,在点击添加按钮时添加Name
, Price
, Quantity
,动态字段。Sub Total
我的目标是从多个动态字段中添加所有小计并将其显示在不同的字段中。
解决方案
您可以使用each
循环遍历所有输入,即:sub_total
然后获取输入值并将其添加到每次迭代的某个变量中,最后将其附加到某个元素。
演示代码:
$(document).on('change', '.qty', function() {
var quantity = $(this).val();
var sub = $(this).parent().next().children();
var price = $(this).parent().prev().children().val()
var subTotal = parseInt(quantity) * parseInt(price);
sub.val(subTotal);
calculate_total(); //call to calculate
});
var i = 1;
$(function() {
$("#btnAdd").bind("click", function() {
var div = $("<tr />");
div.html(GetDynamicTextBox(""));
$("#TextBoxContainer").append(div);
});
$("body").on("click", ".remove", function() {
$(this).closest("tr").remove();
calculate_total(); //call to calculate
});
});
function GetDynamicTextBox(value) {
i++;
return '<td><select class="form-control select2 select-service" style="width: 100%;" id="service-select' + i + '" required name="product[]"><option value="">--Select Product--</option></select></td>' + '<td><input class="form-control rate" value="10" name = "rate[]" type="text" placeholder="Rate" id="rate1' + i + '" required readonly/></td>' + '<td><input class="form-control qty" placeholder="Quantity" name = "qty[]" type="number" id="qty1' + i + '" /></td>' + '<td><input class="form-control sub_total" placeholder="Sub Total" name = "sub_total[]" type="text" id="sub_total1' + i + '" readonly required/></td>' + '<td><button type="button" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove-sign">x</i></button></td>'
}
function calculate_total() {
var total = 0;
//loop through all inputs(sub-total)
$("input.sub_total").each(function() {
//check if value is not null
total += ($(this).val() != "") ? parseInt($(this).val()) : 0;
})
//add total
$("#total").text(total)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-responsive table-striped table-bordered">
<thead>
<tr>
<td>Name</td>
<td>Price</td>
<td>Quantity</td>
<td>Sub Total</td>
<td><button id="btnAdd" type="button" class="btn btn-primary" data-toggle="tooltip" data-original-title="Add more controls"><i class="glyphicon glyphicon-plus-sign"></i> Add </button></td>
</tr>
</thead>
<tbody id="TextBoxContainer">
<tr>
<td>
<select class="form-control select-service" style="width: 100%;" id="service-select" required name="service[]">
<option value="">--Select Services--</option>
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
</select>
</td>
<td><input type="text" class="form-control rate" placeholder="Rate" readonly name="rate[]" value="10" id="rate1" required></td>
<td><input type="number" min="1" class="form-control qty" placeholder="Quantity" name="qty[]" id="qty1"></td>
<td><input type="text" min="1" class="form-control sub_total" placeholder="Sub Total" name="sub_total[]" readonly id="sub_total1"></td>
<td><button type="button" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove-sign">x</i></button></td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="3">Total : </th>
<th id="total">0</th>
</tr>
</tfoot>
</table>
推荐阅读
- position - aframe 获取光标位置并在该位置设置实体
- selenium-webdriver - Javascript 仪表 - 类中的 webdriverjs 执行钩子
- android - 如何在 Ubuntu 18 上运行的 Virtual Box 中为 Androidx86 8.1rc2 VM 启用蓝牙
- javascript - javascript - 导出默认类型
- github - 如何在不破坏分叉或 PR 的情况下将带有分叉的公共回购变成私人回购?
- excel - 如何从混合 Excel 单元格中提取带前导零的数字?
- terraform - 是否可以使用 terraform provider vsphere 克隆现有的 VM(不是模板)
- objective-c - iOS 允许原生 html5 播放器进入横向模式
- button - 将用户鼠标移动到确定/取消按钮的好习惯?
- cron - 如何最好地将数据/RSS 提要添加到 MongoDB Atlas 服务器?