简体   繁体   English

使用javascript与表达式进行比较

[英]comparison with expression using javascript

here is my code 这是我的代码

   function isDepositGreaterThanAvailable(){
   var deposit=$("#total").val();
   var available=$("#availableCash").val();

   if(deposit>available){
       $("#total").val(0);
       var msg = {
           "class": "com.docu.common.Message",
           "messageBody": ["Deposit must be less than or equal to Available Cash"],
           "messageTitle": "Message",
           "type": 2
       }
       MessageRenderer.render(msg);
       return false;
   }

   }

if deposit is greater than available then an error message shows.but when deposit is smaller than available,it shows error too.please help me to solve it. 如果deposit大于available deposit ,则会显示错误消息。但是,如果deposit smaller可用deposit也会显示错误。请帮助我解决。

When you get a value out of an input element using jQuery .val() , it will always be a string. 当您使用jQuery .val()从输入元素中获取值时,它将始终是字符串。 It doesn't matter that the input element is type="number". 输入元素为type =“ number”没关系。 The JavaScript value is a string. JavaScript值是一个字符串。 Use the following if condition: 如果满足条件,请使用以下内容:

if(parseInt(deposit) > parseInt(available))

EDIT: 编辑:
As pointed rightly by @nnnnnn, you might want to use parseFloat() as you are dealing with currency. 正如@nnnnnn正确指出的那样,您在处理货币时可能要使用parseFloat()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM