检查数字是否在范围内
问题描述:
我正在检查来自输入字段的号码是否在范围内
I am checking if my number coming from a input field is in range
function timeCheck(){
var time = $.trim($('#enterTime').value());
Number.prototype.between = function(min,max){
return this > min && this < max;
};
if ((time).between(1,9)){
alert("test");
}
}
但不知何故它不起作用..警报是永远不会被触发
But somehow it does not work .. the alert is never triggered
感谢您的帮助和快速回答
Thanks for help and fast answer
答
扩展@ Daniel的回答是,还有另外两个错误:第一, $('#enterTime')。value()
不是一个有效的jQuery函数,应该是 $( '#enterTime')。VAL()
。其次,您需要将值转换为数字
。否则,您将尝试访问字符串的属性之间的,该属性不存在。最终的代码是:
Extending @Daniel's answer, there are two other errors: first, $('#enterTime').value()
is not a valid jQuery function, should be $('#enterTime').val()
. Second, you need to convert your value to type Number
. Otherwise, you will be trying to access the between
property of a string, which doesn't exist. The final code would be:
function timeCheck(){
var time = new Number($.trim($('#enterTime').val()));
Number.prototype.between = function(min,max){
return this > min && this < max;
};
if(time.between(1,9)){
alert("test");
}
}