0

I'm having a period mental melt down here. For 2 days, I've been looking at numbers and trying to get this damn calculation to work, but now i can't get the result method to work.

I'm having 1 textboxs, 2 checkboxs, 1 dropdown and a h1 where the result will be shown.

What i need help with is the calculation and the showing of the result. Heres the JQuery code I'm having.

    var iAlt = 0;

    // DROPDOWN
    var halloAbo = "";
    $('#hallo-abo').change(function (){
        halloAbo = $('#hallo-abo').val();   
            //$('#sk').text(halloAbo);
    });


    // TEXTBOX
    var maxregning = 0;
    $('#maxRegning').focusout(function(){
        maxregning = 0;
        maxregning = $('#maxRegning').val();

        $('#sk').text(maxregning);
    });



    // SELECT 1
    var pakkerTotal = 0;
    $('#pakker :checkbox').click(function() {
        pakkerTotal = 0;
        $('#pakker :checkbox:checked').each(function(idx, elm) {
            pakkerTotal += parseInt(elm.value, 10);
        });
        //visResultat();
    });

    // SELECT 2
    var betalTotal = 0;
    $('#betal :checkbox').click(function() {
        betalTotal = 0;
        $('#betal :checkbox:checked').each(function(idx, elm) {
            betalTotal += parseInt(elm.value, 10);
        });
        //visResultat();

    });

    // NEED HELP HERE
    var buffer1 = 0;
    var buffer2 = 0;
    function visResultat(){
        iAlt = eval(maxregning -= pakkerTotal -= betalTotal);
        $('#sk').text(iAlt)
    }


    //$('#sk').text(iAlt);

Heres some of the HTML

// DROPDOWN    
<select id="hallo-abo" style="float:left;">
<option value="">Vælg dit abo</option>
<option value="49">test 1</option>
<option value="99">test 2</option>
ect
</select>

// TEXTBOX
<input type="textbox" id="maxRegning" value="" />


// CHECKBOX 1
<div id="pakker">
<input type="checkbox" value="39" />test 1<br/>
<input type="checkbox" value="79" />test 2<br/>
</div>

//CHECKBOX 2
<div id="betal">
<input type="checkbox" value="6,5" />test 1<br/>
<input type="checkbox" value="36" />test 2
</div>

// RESULT
<h1 id="sk">0</h1>

Hope some one can help me here

Patrick R
  • 1,949
  • 3
  • 32
  • 58

1 Answers1

1

This looks very wrong;

iAlt = eval(maxregning -= pakkerTotal -= betalTotal);

I suspect you just wanted:

iAlt = maxregning - pakkerTotal - betalTotal;

First point: The -= operator actually changes the value on the left, rather than just returning the result of the calculation. So what you line was doing was actually changing maxregning and pakkerTotal.

Second: You were doing some arithmetic, and then "eval"ing the result. This is meaningless; if the maths came out to 12, it would eval 12.toString(), which is "12". eval("12") is 12.

Thirdly: Eval is almost always the wrong answer ;) If you think you need to eval() anything, stop and think about it, and then don't do it.

N3dst4
  • 6,360
  • 2
  • 20
  • 34
  • Thanks a lot! I was just looking at some other examples and they used eval, so though i needed it, so thanks for the tip :) – Patrick R Nov 10 '11 at 11:17