简体   繁体   English

javascript将常量添加到变量

[英]javascript adding a constant to a variable

I'm trying to validate user entered values as being within a range that is equal to a prior user entered number plus/minus constant numbers but the script seems to only recognize the lower bound of the range and the upper bound is always the prior user entered number plus any value less than one (aka user entered 55 so 55.999999 works but 56 turns red). 我正在尝试验证用户输入的值是否在等于先前用户输入的数字加/减常数的范围内,但是脚本似乎只能识别该范围的下限,而上限始终是先前的用户输入的数字加上任何小于1的值(又名用户输入了55,因此55.999999可以工作,但56变成红色)。 Code: 码:

if (BUNO.rawValue>=164865){ 
    if (Auto1Right.rawValue>=45 && Auto1Right.rawValue<=75)
    {
        var eleven = 11.00;
        var auto2min=Auto1Right.rawValue-eleven;
        var five = 5.70; 
        var auto2max=Auto1Right.rawValue+five;
        if (this.rawValue>=auto2min&&Man2Right.rawValue<=auto2max)
        {
            Man2Right.fillColor="0,255,0";
        }


    }
    else
    {
        Man2Right.fillColor="255,0,0";
    }

}

I think you might just have a small typo. 我认为您可能只有一个小错字。 You have two equal signs here 你这里有两个等号

var auto2max==Auto1Right.rawValue+five;

It should probably look like this: 它可能看起来应该像这样:

var auto2max = Auto1Right.rawValue + five;

apparently this can be avoided by changing the "+" symbol to a "-" symbol and changing your variable value to a negative number. 显然,可以通过将“ +”符号更改为“-”符号并将变量值更改为负数来避免这种情况。

i have no idea why it won't use the "+" for addition without changing the variable to a string. 我不知道为什么在不将变量更改为字符串的情况下为什么不使用“ +”进行添加。 According to everything i can find the "+" symbol should add both numbers and strings. 根据我能找到的一切,“ +”符号应同时添加数字和字符串。

It sounds like the rawValue is being interpreted as a string, not a number. 听起来好像rawValue被解释为字符串,而不是数字。 Have you tried casting your variables ? 您是否尝试过转换变量

if ( parseFloat(BUNO.rawValue) >= 164865 ){
    var a1r = parseFloat(Auto1Right.rawValue)
    if (a1r>=45 && a1r<=75)
    {
        var eleven = 11.00;
        var auto2min=Auto1Right.rawValue-eleven;
        var five = 5.70; 
        var auto2max = a1r + five;

        var thisRV = parseFloat(this.rawValue) ;
        var m2RV = parseFloat(Man2Right.rawValue) ;
        if (thisRV>=auto2min && m2RV<=auto2max)
        {
            Man2Right.fillColor="0,255,0";
        }


    }
    else
    {
        Man2Right.fillColor="255,0,0";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM