我有以下 html 字段,我需要检查输入值是 float 还是 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中检查一个值是float还是int呢?
我需要查找/检查这两种情况,无论是浮点数还是整数,因为稍后如果该值是float
我会将它用于某种目的,类似地int
.
use typeof
检查类型,然后value % 1 === 0
如下所示识别 int,
if(typeof value === 'number'){
if(value % 1 === 0){
// int
} else{
// float
}
} else{
// not a number
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)