LearningPHP
LearningPHP

Reputation: 17

calculating sum of dropdown list

I have a drop down list here :

http://jsfiddle.net/HcPYM/

can't I avoid using the (Distributor) and the (Public) buttons and just show a text automatically appears under the table ?

and if I can't .. can I just add (CHF) beside the number that appears in the message ?

that's the jquery I use

eval( $( 'td:nth-child(3)' ).map( function( td, $this ){
    return ( ( parseFloat( ( $this = $( this ) ).text() ) || 0 )
        * ( parseFloat( $this.parent().find( 'select' ).val() ) || 0 ) );
} ).get().join( '+' ) )

Please explain in details cuz I'm super beginner

Upvotes: 0

Views: 224

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191729

Instead of using onclick for the Distributor/Public buttons, you can just update the table (or alert) on the change event of the dropdowns. The only thing is that how do you know whether they are calculating with "Distributor" or "Public?"

$(".pricestable select").on('change', calculate);

http://jsfiddle.net/HcPYM/2/

I suppose that "Distributor" and "Public" could be radio buttons, and you could make the decision based on that:

$(".pricestable select").on('change', function () {
    if ($("#distributor").prop('checked')) {
        calculate();
    }
    else {
        calculate2();
    }
});

I also advise against using eval -- you can just iterate over the arrays and add them using + instead of evaluating the string with +es.

Upvotes: 1

Related Questions