2017-04-16 99 views
0

我有三個輸入值,一個用於付款,第二個用於預付款 ,第三個用於從actual_payment到advance_payment的剩餘餘額。Onkey up第二個輸入值不應大於第一個值

如果預付款超過實際付款,則不應輸入預付款。

<input type="text" id="actual_payment" value=""> 
<input type="text" id="advance_payment" value=""> 
<input type="text" id="balance_payment" value=""> 

如何比較預付款不應超過實際付款餘額不應超過實際付款和預付款。

這裏是我的jQuery代碼

$(function(){ 
     $("#advance_payment").on('keyup', function(){ 
      var actual_payment = $("#actual_budget").val(); 


      $("#balance_payment").val(actual_payment-$(this).val());  
      if ($("#advance_payment").val()>$("#actual_budget")) { 
       alert("sorry the advance payment should be less actual payment"); 
      } 

     }) 
     }); 
+1

你可能會在結果中得到'NaN',因爲你減去了字符串,試着改變這些輸入的類型到'number'(或者用'parseInt'解析這個值),它也應該是'if ($(「#advance_payment」).val()> $(「#actual_budget」).val())' –

+0

實際上,在if()中代替'$(「#actual_budget」)'是'actual_payment'。 –

回答

0

下返回一個字符串:

$("#actual_budget").val() 

你可能想使用parseFloat()功能:

parseFloat($("#actual_budget").val()); 

這將適用於任何字段,您檢索爲十進制數字。

相關問題