2015-01-15 17 views
0

我有一個帶有2個提交按鈕的窗體。一個是Approve另一個是RejectJavascript數值變量比較不起作用

HTML is

<form action="test.php" method="post" name="sos_submit" id="sos_submit"> 

    <input type="text" name="qty_indented" value="<?php echo $qty_indented ?>" id="qty_indented" readonly="readonly" /> 
    <input type="text" name="qty_approved" value="<?php echo $qty_approved ?>" id="qty_approved" /> 
    <input type="submit" name="da_approve" value="APPROVE" onclick="return submitForm(this)" /> 
    <input type="submit" name="da_reject" value="REJECT" onclick="return submitForm(this)" /> 

</form> 

而且我submitForm(this)功能樣子:

<script> 
function submitForm (button){ 

    if (button.value == "APPROVE"){ 
     var qty_approved = document.getElementById("qty_approved").value; 
     var qty_intended = document.getElementById("qty_indented").value; 
     if (qty_approved <= 0){ 
      alert("Please Enter the Quantity for Approval!"); 
      submitOK = "false"; 
     } 
     else if(qty_approved >= qty_intended){ 
      alert("Quantity Approved " + qty_approved + " Cannot be more than quantity intended " + qty_intended + " !"); 
      submitOK = "false"; 
     } 
     if (submitOK == "false") { 
      return false; 
     } 
    } 
    else{ 
     confirm("Are you sure you want to REJECT the Voucher?");  
    } 
} 
</script> 

雖然它非常簡單的代碼,但不知何故,無法正常工作。

即使qty_approvedLESS而不是qty_indentedif()語句正在執行。

例如,如果qty_approved = 5qty_indented = 10,那麼代碼應該提交表單,但它不是。它顯示:Quantity Approved 5 Cannot be more than quantity intended 10 !

我做錯了什麼?

+1

'.value'返回一個**字符串**。 'qty_approved> = qty_intended'執行字符串比較。 – 2015-01-15 16:36:32

+1

他們被比較爲字符串 - 嘗試使它們爲int。 – Soren 2015-01-15 16:37:12

回答

4

輸入框是基於字符串的值。你需要使用parseInt將值轉換爲整數:

var qty_approved = parseInt(document.getElementById("qty_approved").value, 10); 
var qty_intended = parseInt(document.getElementById("qty_indented").value, 10); 

目前它比較「5」到「10」和字母「5」是不是「10」更大,這就是爲什麼它拖放到該條件。

+0

這實際上解決了問題!當我允許時我會投票。 – aki2all 2015-01-15 16:41:49