jQuery 如果值介于两个数字之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4408676/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:14:16  来源:igfitidea点击:

if value is between two numbers

jqueryif-statementdate-range

提问by Inigo

I want to be able to test whether a value is within a number range. This is my jQuery code...

我希望能够测试一个值是否在一个数字范围内。这是我的jQuery代码...

if ((year < 2099) && (year > 1990)){
    return 'good stuff';
}

Is there a simpler way to do this in jQuery? For example, is there something like this...

在 jQuery 中有没有更简单的方法来做到这一点?例如,有没有这样的...

if (1990 < year < 2099){
    return 'good stuff';
}

回答by Mark Rushakoff

In many languages, the second way will be evaluated from left to right incorrectly with regard to what you want.

在许多语言中,第二种方式会根据您想要的东西从左到右错误地进行评估。

In C, for instance, 1990 < yearwill evaluate to 0 or 1, which then becomes 1 < 2099, which is always true, of course.

例如,在 C 中,1990 < year将计算为 0 或 1,然后变为1 < 2099,当然,这始终为真。

Javascript is a quite similar to C: 1990 < yearreturns trueor false, and those boolean expressions seem to numerically compare equal to 0 and 1 respectively.

Javascript 与 C: 1990 < yearreturns trueor非常相似false,这些布尔表达式似乎在数字上分别等于 0 和 1。

But in C#, it won't even compile, giving you the error:

但在 C# 中,它甚至不会编译,给你错误:

error CS0019: Operator '<' cannot be applied to operands of type 'bool' and 'int'

错误 CS0019:运算符“<”不能应用于“bool”和“int”类型的操作数

You get a similar error from Ruby, while Haskell tells you that you cannot use <twice in the same infix expression.

你从 Ruby 得到一个类似的错误,而 Haskell 告诉你不能<在同一个中缀表达式中使用两次。

Off the top of my head, Python is the only language that I'm sure handles the "between" setup that way:

在我的脑海中,Python 是唯一一种我确定以这种方式处理“之间”设置的语言:

>>> year = 5
>>> 1990 < year < 2099
False
>>> year = 2000
>>> 1990 < year < 2099
True

The bottom line is that the first way (x < y && y < z)is always your safest bet.

最重要的是,第一种方式(x < y && y < z)始终是您最安全的选择。

回答by Max Sylvester

You could make your own method:

你可以制定自己的方法:

// jquery
$(function() {
    var myNumber = 100;
    try {
        if (myNumber.isBetween(50, 150)) 
            alert(myNumber + " is between 50 and 100.");
        else 
            alert(myNumber + " is not between 50 and 100.");
    } catch (e) {
        alert(e.message());
    }

});

// js prototype
if (typeof(Number.prototype.isBetween) === "undefined") {
    Number.prototype.isBetween = function(min, max, notBoundaries) {
        var between = false;
        if (notBoundaries) {
            if ((this < max) && (this > min)) between = true;
            alert('notBoundaries');
        } else {
            if ((this <= max) && (this >= min)) between = true;
            alert('Boundaries');
        }
        alert('here');
        return between;
    }
}

hope this helps.

希望这可以帮助。

Max

最大限度

回答by khalid

The fast and simple way to make this is to create a function like this:

实现这一点的快速而简单的方法是创建一个像这样的函数:

function inRange(n, nStart, nEnd)
{
    if(n>=nStart && n<=nEnd) return true;
    else return false;
}

Then use that as follows:

然后按如下方式使用它:

inRange(500, 200, 1000) => this return true;

Or like this:

或者像这样:

inRange(199, 200, 1000) => this return false;

回答by Allan Taylor

From a similar solution here: http://indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is-between-two-numbers/

从这里的类似解决方案:http: //indisnip.wordpress.com/2010/08/26/quicktip-check-if-a-number-is-between-two-numbers/

$.fn.between = function(a,b){
    return (a < b ? this[0] >= a && this[0] <= b : this[0] >= b && this[0] <= a);
}

回答by dckrooney

If you don't like the boolean operator, you could always use nested if statements:

如果您不喜欢布尔运算符,您可以始终使用嵌套的 if 语句:

if (1990 < year)
{
    if( year < 2099)
        return 'good stuff';
}