kosbou
kosbou

Reputation: 3999

jquery strange button behavior

I am in mobile app. I use a button that on click checks if gtotal is greater than total and alerts a message. Here is my code

        $("#pay").unbind('click').click(function(e) {
            barcodeTotal();
            if (gtotal > total){
                alert("Too much money");
                $('#shares').val('');
                payment = 0;
                gtotal = 0; 
                $("#Payment").html('<p class="total">Total Payment: <strong>' + payment.toFixed(2) + '</strong></p>');
                return false;
            }
            updateItem(payment.toFixed(2), gtotal, barcode, barcodeamount, barcodeprevious, allbarcode);
        });

I have the following problem. The alert pops up on every click without if statement check...

Where am i wrong?

please advice

this is total calculation

        for (i = 0; i < len; i += 1) {
            row = resultflatname.rows.item(i);
            if (row.receiptno == 0){
                items.push('<input type="checkbox" name="code_'+ i +'" id="code_'+ i +'" value="' + row.amount + '" previous="' + row.pastpayments + '" barcode="' + row.barcode + '" todayp="' + row.todaypayments + '"/><label for="code_'+ i +'">' + row.period +'..........'+ row.amount+'</label>');
            } else {
            if ((row.receiptno > 0) && (row.amount > row.todaypayments + row.pastpayments)){
                items.push('<input type="checkbox" name="code_'+ i +'" id="code_'+ i +'" value="' + row.amount + '" previous="' + row.pastpayments + '" barcode="' + row.barcode + '" todayp="' + row.todaypayments + '"/><label for="code_'+ i +'">' + row.period +'..........'+ row.amount+'</label>');
            }
        }
                allbarcode[i] = row.barcode;
                previouspayments1 = previouspayments1 + row.pastpayments;
                previouspayments = previouspayments1.toFixed(2);
                sofeilon1 = sofeilon1 + row.amount;
                sofeilon = sofeilon1.toFixed(2);
                total1 = sofeilon - previouspayments;
                total = total1.toFixed(2);

and this is gtotal calculation

        $('#shares').keyup(function(){
            payment = 0;
            calcTotal();
            gtotal = ($('#shares').val() * 1) + payment;
            gtotal = gtotal.toFixed(2);
            $("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
        });


        $('#shares').keyup(function(){
            $("input:checkbox, input:radio").click(function() {
                payment = 0;
                calcTotal();
                gtotal = ($('#shares').val() * 1) + payment;
                gtotal = gtotal.toFixed(2);
                $("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
            });
        }); 

and calctotal function

        function calcTotal() {
            $("input:checked").each(function() {
                var value = [$(this).attr("value")]-[$(this).attr("previous")];
                payment = payment + parseFloat(value); //total = total + value          
            });
        }

Upvotes: 0

Views: 77

Answers (1)

Stefan
Stefan

Reputation: 5662

You should debug the variables gtotal and total to view their data types.

console.log(gtotal, typeof(gtotal));
console.log(total, typeof(total));

If the values are strings the if-statement will return true as "76.40" is considered greater than "107.10".

Use parseFloat() to convert the strings to numbers.

Upvotes: 1

Related Questions