jeudi 9 mars 2017

jQuery - Adding multiple values together is checkbox is checked

I have a quote form on a page (made using Gravity Forms WP plugin, but that doesn't matter). The form has multiple checkboxes on it - call these options. Each option has a different value 'assigned' to it. What I want to do, is add these values together if that particular checkbox is ticked, and subtract it's value if it's not. I hope that makes sense...?

Here is what I have so far:

jQuery(document).ready( function() {
    jQuery('#choice_2_10_4, #choice_2_10_5').change( function() {
        var option1 = Number( jQuery('#choice_2_10_4:checked').val() || 0 ); // Add 250 if checked
        var option2 = Number( jQuery('#choice_2_10_5:checked').val() || 0 ); // Add 200 if checked
        jQuery('#quote-price').val( option1 + option2 );
    });
});

quote-price is the field I'd like to show the total of the calculation in.

Can i give #choice_2_10_4 and #choice_2_10_5 a value by changing them to:

jQuery('#choice_2_10_4:checked').val(250)
jQuery('#choice_2_10_5:checked').val(250)

Or something along those lines?

Thanks




Aucun commentaire:

Enregistrer un commentaire