mardi 5 novembre 2019

How can I calculate selected checkbox foreach only

I need to calculate selected checkbox only that foreach in a table field (harga_obat*jumlah)

This is my foreach data

@foreach ($obat as $o)
<tr>
    <td>
        <input id="check" type="checkbox" name="select[]" value="">
    </td>
    <td>
        <input type="text" class="form-control" name="kode_obat" value="" readonly>
    </td>
    <td>
        <input type="text" class="form-control" name="nama_obat" value="" readonly>
    </td>
    <td>
        <input id="harga" type="text" class="form-control" name="harga_obat" value=""
            onkeyup="sum()">
    </td>
    <td>
        <input id="jumlah" type="text" class="form-control" name="jumlah" onkeyup="sum()">
    </td>
    <td>
        <input id="total" type="text" class="form-control total-harga" name="total_harga" onkeyup="sum()" readonly>
    </td>
</tr>
@endforeach

This is what I have tried, this is just working for one row like picture below

function sum() {
    var harga = document.getElementById('harga').value;
    var jumlah = document.getElementById('jumlah').value;
    var hasil = parseInt(harga) * parseInt(jumlah);
    if (!isNaN(hasil)) {
        document.getElementById('total').value = hasil;
    }
}

enter image description here




Aucun commentaire:

Enregistrer un commentaire