2012-02-24 93 views
0

我在移動應用程序。我使用一個按鈕,點擊檢查gtotal是否大於總數並提醒消息。這裏是我的代碼jquery奇怪的按鈕行爲

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

我有以下問題。在沒有語句檢查的情況下,每次點擊都會彈出提示...

我在哪裏錯了?

請指教

這是總的計算

 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   
      }); 
     } 
+0

向我們展示您定義gtotal和total的位置,因爲gtotal總是>總數> – 2012-02-24 16:09:05

+0

其中gtotal和total的計算結果是?請在代碼中顯示它們的計算位置。 – lamplightdev 2012-02-24 16:09:41

+0

我們需要更多信息來幫助你。 gtotal和total集合在哪裏? – jmaglio 2012-02-24 16:09:59

回答

1

你應該調試VA ribles gtotaltotal查看其數據類型。

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

如果值是字符串if語句將返回true爲「76.40」被認爲比「107.10」較大。使用parseFloat()將字符串轉換爲數字。

+0

thx now it works – kosbou 2012-02-24 17:33:17