试试这个:

if(Math.floor(id) == id && $.isNumeric(id))
alert('yes its an int!');

$.isNumeric(id)检查它是否是数字
然后,Math.floor(id) == id将确定它是否真的是整数值而不是浮点数。如果它是浮点数,那么将它解析为 int 将得到与原始值不同的结果。如果它是 int,两者将是相同的。

使用 jQuery 的 IsNumeric 方法。

Http://api.jquery.com/jquery.isnumeric/

if ($.isNumeric(id)) {
//it's numeric
}

更正: 这不能保证 整数。这将:

if ( (id+"").match(/^\d+$/) ) {
//it's all digits
}

当然,这并不使用 jQuery,但是我假设只要解决方案能够正常工作,jQuery 实际上并不是强制性的

下面是 Number谓词函数的填充函数:

"use strict";


Number.isNaN = Number.isNaN ||
n => n !== n; // only NaN


Number.isNumeric = Number.isNumeric ||
n => n === +n; // all numbers excluding NaN


Number.isFinite = Number.isFinite ||
n => n === +n               // all numbers excluding NaN
&& n >= Number.MIN_VALUE  // and -Infinity
&& n <= Number.MAX_VALUE; // and +Infinity


Number.isInteger = Number.isInteger ||
n => n === +n              // all numbers excluding NaN
&& n >= Number.MIN_VALUE // and -Infinity
&& n <= Number.MAX_VALUE // and +Infinity
&& !(n % 1);             // and non-whole numbers


Number.isSafeInteger = Number.isSafeInteger ||
n => n === +n                     // all numbers excluding NaN
&& n >= Number.MIN_SAFE_INTEGER // and small unsafe numbers
&& n <= Number.MAX_SAFE_INTEGER // and big unsafe numbers
&& !(n % 1);                    // and non-whole numbers

所有主流浏览器都支持这些函数,除了 isNumeric,它不在规范中,因为它是我编造的。因此,你可以减少这种填料的大小:

"use strict";


Number.isNumeric = Number.isNumeric ||
n => n === +n; // all numbers excluding NaN

或者,只需手动内联表达式 n === +n