Possible Duplicate:
Check if a variable contains a numerical value in Javascript?

How do I check if variable is an integer in jQuery?

例子:

if (id == int) { // Do this }

Im using the following to get the ID from the URL.

var id = $.getURLParam("id");

But I want to check if the variable is an integer.

推荐答案

Try this:

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

$.isNumeric(id) checks whether it's numeric or not
Math.floor(id) == id will then determine if it's really in integer value and not a float. If it's a float parsing it to int will give a different result than the original value. If it's int both will be the same.

Jquery相关问答推荐

如何在外部JS文件中使用带有参数的laravel路由

获取 html 元素的 onclick 事件的 data-* 属性

如何像这样通过 $.ajax ( serialize() + extra data ) 添加数据

Jquery如何通过数组中的属性查找对象

如何以十六进制获取元素的背景 colored颜色 代码?

jQuery 绑定点击 *ANYTHING* 但 *ELEMENT*

使用 jQuery 按文本内容 Select 选项

使用 jQuery 发送 JSON 数据

Backbone.js 和 jQuery

jquery-ui datepicker更改z-index

使用 jQuery Select 焦点文本在 Safari 和 Chrome 中不起作用

使用 jQuery 获取选定的选项 id

移动 chrome 在滚动时触发调整大小事件

将图像 url 转换为 Base64

如何签入用户已在 Google recaptcha 中选中复选框的 js?

jQuery确定匹配的类是否具有给定的ID

如何在 jQuery 中 Select this中的元素?

如何在纯 JavaScript 中模拟鼠标悬停以激活 CSS:悬停?

在事件中使用 jQuery 获取被点击的元素?

如何以编程方式触发 ngClick