jquery奇怪的按钮行为

时间:2012-02-24 16:06:55

标签: jquery button if-statement

我在移动应用中。我使用点击检查按钮,如果gtotal大于总数并提醒消息。这是我的代码

        $("#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);
        });

我有以下问题。没有if语句检查,每次点击都会弹出警报......

我哪里错了?

请咨询

这是总计算

        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);

这是gtotal计算

        $('#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>");
            });
        }); 

和calctotal函数

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

1 个答案:

答案 0 :(得分:1)

您应该调试变量gtotaltotal以查看其数据类型。

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

如果值是字符串,则if语句将返回true,因为“76.40”被认为大于“107.10”。

使用parseFloat()将字符串转换为数字。