概述 jQuery.isNumeric( value )
返回值:Boolean
描述:确定它的参数是否是一个JavaScript数字。
$.isNumeric()
方法检查它的参数是否能代表一个数值。如果是这样,则返回true
。否则返回false
。该参数可以是任何类型。(手册网注:jQuery 3.0之前,这个方法会强制转换参数为Number
,转换后的值类型如果是如果是Number
,也会返回true
)
在jQuery 3.0中,$.isNumeric()
方法只有接收number
类型的参数时候,或者是可以被强制为有限数值的 string
类型的参数,才会返回true
,在其他情况下,返回false
。
示例
以下是使用 $.isNumeric 检测各种输入类型的示例。
- // true (numeric)
- $.isNumeric( "-10" )
- $.isNumeric( "0" )
- $.isNumeric( 0xFF )
- $.isNumeric( "0xFF" )
- $.isNumeric( "8e5" )
- $.isNumeric( "3.1415" )
- $.isNumeric( +10 )
- $.isNumeric( 0144 )
- // false (non-numeric)
- $.isNumeric( "-0x42" )
- $.isNumeric( "7.2acdgs" )
- $.isNumeric( "" )
- $.isNumeric( {} )
- $.isNumeric( NaN )
- $.isNumeric( null )
- $.isNumeric( true )
- $.isNumeric( Infinity )
- $.isNumeric( undefined )