dimanche 21 février 2016

Calculate value of text input where checkbox is selected

I am trying to get the value of a text input where the select box next to it is selected.

I've got to somehow join the checkbox and text-input together, so it knows only calculate the value of the text input where the checkbox on the same table row is selected.

An issue is also the fact that none of my checkboxes get the attribute of 'checked' when selected either, so I've not been able to target those which are :checked too.

I suspect my method so far is going a little too over the top so any suggestions are welcome.

http://ift.tt/1QtlT81

$('table.table-bordered input[type="checkbox"]').change(function () {
    $(this).toggleClass('jordan');
});

$('#button-cart').hover(function () {
    var sum = 0;
    $('table.table-bordered input[type="checkbox"]:checked').each(function() {
        $('table.table-bordered input[type="text"].jordan').each(function() {
            sum += Number($(this).val());
        });
    });
    console.log(sum);
});

The Goal: on 'Add to Basket' click, check at least one of the selected options is selected (else alert), if one is selected, calculate the total quantity from the inline input fields (where checkbox is selected) (for now simply alert the total).

enter image description here

In this example, the value to be alerted would be 5.




Aucun commentaire:

Enregistrer un commentaire