在jquery中检查值是float还是int [英] Check a value is float or int in jquery

查看:332
本文介绍了在jquery中检查值是float还是int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下html字段,我需要检查输入值是浮点数还是int,

I have the following html field, for which i need to check whether the input value is float or int,

<p class="check_int_float" name="float_int" type="text"></p>


$(document).ready(function(){
   $('.check_int_float').focusout(function(){

       var value  = this.value
       if (value is float or value is int)
          {
           // do something
          }      
       else
          {
           alert('Value must be float or int');   
          }  

   });

});

那么如何在jquery中检查值是浮点数还是int值。

So how to check whether a value is float or int in jquery.

我需要查找/检查两种情况,无论是浮点数还是int,因为稍后如果值为 float 我将使用它出于某种目的,同样适用于 int

I need to find/check both cases, whether it is a float, or int, because later if the value was float i will use it for some purpose and similarly for int.

推荐答案

使用 typeof 检查类型,然后 value%1 === 0 将int标识为以下,

use typeof to check the type, then value % 1 === 0 to identify the int as bellow,

if(typeof value === 'number'){
   if(value % 1 === 0){
      // int
   } else{
      // float
   }
} else{
   // not a number
}

这篇关于在jquery中检查值是float还是int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆