mercredi 20 septembre 2017

How to calculate total sum of checkbox values dend on quantity in textbox

I have the following table

<table>
<tr style="background-color: silver;">
    <th>Check it</th>
    <th>Estimate item</th>
    <th>Quantity</th>
    <th>Cost</th>               
</tr>
<tr>
   <td><input type="checkbox" value="450" /></td>
   <td>Remove Tile</td>
   <td><input type="text" value="1"></td>
   <td>$450</td>
</tr>
<tr>
   <td><input type="checkbox" value="550" /></td>
   <td>Remove Tub</td>
   <td><input type="text" value="1"></td>
   <td>$450</td>
</tr>

Table example I found how to calculate the sum of checkboxes values:

<script>
        $(document).ready(function () {
            var $inputs = $('input[type="checkbox"]')
            $inputs.on('change', function () {
                var sum = 0;
                $inputs.each(function() {
                   if(this.checked)
                       sum += parseInt(this.value);
                });
                $("#price").val(sum);
            });
        });
</script>

The question is: If user will update the quantity in textbox, i need to update the total. I want it to work in two ways: quantity changed before or after checkbox has been checked.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire