2014-10-08 86 views
0

我有一個操作,如果鎮靜,它不工作真正在這一行if(score> tot){ 我有兩個數字在這些變量它不檢查真實和總數大於得分但返回假不工作。JQuery>操作不起作用

$(document).ready(function(){ 
     $(".saveMarkList").click(function(event){ 
      var score = $(this).closest('tr').find('input').val(); 
      var total = $(this).closest('tr').find('label').text(); 
      var tot = $.trim(total);       
       if(score > tot){ 
       return false;     
       }else if(score == ''){ 
       return false;     
       }else if(tot == ''){ 
       return false;     
       }     
      var row = $(this).closest('tr'); 
      var savebtnTD = $(this).closest('tr').find('span'); 
      var subjectId = $(".suid").prop('id'); 
      var senfId = $(".senfid").prop('id'); 
      var studentId = $(this).closest('tr').find('td').next('td').prop('id'); 
      var assesmentId = $(this).closest('tr').find('select').val(); 



      var url = "<?php echo base_url().'examination/saveMarkList';?>"; 

      $.ajax({ 
       url: url, 
       data: {'subjectId': subjectId, 'senf_id':senfId, 'studentId': studentId, 'assId': assesmentId, 'score': score}, 
       type: 'post', 
       success: function(result){ 
        if(result){ 
         //row.css('border', '2px solid green'); 
         savebtnTD.css('border', '2px solid green; color: green;').html('<span>دخیره شد</span>'); 
        }else{ 
         alert(result); 
        } 
       }, 
       error: function(){ 
        alert("Some technical problem"); 
       } 
      }); 
     }); 
    }); 
+0

你能舉一個總分和總分的例子嗎? – Spokey 2014-10-08 11:50:26

+0

當我把變量提醒它顯示我真實的我的號碼,但在檢查其不工作 – 2014-10-08 11:52:47

+0

*總數大於分數*那麼條件是正確的,分數不會大於總數。你是不是指'if(tot> score){' – Spokey 2014-10-08 11:52:50

回答

1

您需要的值作爲數字,而不是字符串比較。

您可以更快地值使用~~

例如轉換成整數比parseInt

if(~~score > ~~tot) 
1

您需要在比較之前將它們轉換爲int。像這樣,還要添加一個檢查,如果這些字符串是整數或不使用isNaN

if(parseInt(score) > parseInt(tot))

+1

爲了防止某些輸入「錯誤」,還應該使用基數:'parseInt(stringVal,10 )'。 – Sirko 2014-10-08 11:52:38

+0

typo'pareseInt' vs'parseInt'? – charlietfl 2014-10-08 12:07:17

+0

@charlietfl,抱歉錯字錯誤,現在修復!謝謝! – 2014-10-08 12:18:42