2 回答
TA贡献1898条经验 获得超8个赞
您可以使用each循环遍历选中的复选框的值,然后获取HargaSparepartusing的值,.next()因为在您的 html 中它位于checkbox.
演示代码 :
function onChangeCheckbox() {
var rTotal = 0;
var sum = 0;
//loop through ul->checkboxes
$('#sparepartList input[type="checkbox"]:checked').each(function() {
//get next input HargaSparepart
var harga = $(this).next('input[name=HargaSparepart]').val()
rTotal += 1; // add 1
sum += parseInt(harga); //add to sum
})
//add values to inputs
$('#counterSparepart').val(rTotal);
$('#totalBiaya').val(sum);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<label class="control-label col-xs-3">Biaya Total</label>
<div class="col-xs-8">
<input id="totalBiaya" name="biaya" value="" class="form-control" type="number" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-3"></label>
<div class="col-xs-8">
<ul id="sparepartList" style="list-style-type: none; padding: 0;">
<li><input onchange="onChangeCheckbox()" type="checkbox" name="id_sparepart[]" value="' + value.id_sparepart + '">Something Rp. <input id="HargaPart" name="HargaSparepart" value="500" class="form-group" type="text" readonly></li>
<li><input onchange="onChangeCheckbox()" type="checkbox" name="id_sparepart[]" value="' + value.id_sparepart + '"> Something1 Rp. <input id="HargaPart" name="HargaSparepart" value="100" class="form-group" type="text" readonly></li>
<li><input onchange="onChangeCheckbox()" type="checkbox" name="id_sparepart[]" value="' + value.id_sparepart + '"> Something2 Rp. <input id="HargaPart" name="HargaSparepart" value="200" class="form-group" type="text" readonly></li>
</ul>
TotalBiaya :
<input type="text" id="counterSparepart">
</div>
</div>
TA贡献1876条经验 获得超6个赞
试试这个代码
$('#HargaPart').change(function (){
var total = 0;
$('#HargaPart:checked').each(function(){ // iterate through each checked element.
total += isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val());
});
$("#total").val(total);
});
添加回答
举报