使用 Jquery 测试字符串是否为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25788900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test if string is Integer with Jquery
提问by Bouraoui KACEM
what is the function which test if a string is an Integer or not like the method :
测试字符串是否为整数的函数是什么,就像方法一样:
jQuery.isNumeric()
i want to test an input html element :
我想测试一个输入的 html 元素:
<input id='distance' type='text' />
thank you in advance
先感谢您
回答by Bouraoui KACEM
thank you for your contributions i found the answer :
感谢您的贡献我找到了答案:
if(Math.floor($('#distance').val()) == $('#distance').val() && $.isNumeric($('#distance').val()))
{
alert('yes its an int!');
}
回答by Blake
I use this
我用这个
function IsInteger(n){
return Number(n) === n && n % 1 === 0;
}
回答by Techy
sorry I didn't notice integer when I answered, here is another type check:
抱歉,我回答时没有注意到整数,这是另一种类型检查:
if($.type($(#distance).val())==="number")
please see: http://api.jquery.com/jquery.type/