Home  >  Article  >  Backend Development  >  How to determine the size of two numerical values ​​in jquery

How to determine the size of two numerical values ​​in jquery

小云云
小云云Original
2018-03-28 14:07:562565browse

This article mainly shares with you how jquery determines the size of two values. It is mainly shared with you in the form of code. I hope it can help you.

$a = 10;
$b = 2;
if($a < $b){
echo  "b大于a";
}else{
echo "a大于b"
};

In js, var defines variables and all generated strings are strings.

var a = 10;
var b = 2;
if(a < b){
alert("错误");
}else{
alert("正常");
};

The operation result is normal; 10 is less than 2, which is normal. Obviously, this is not the result we want. Why? Because both are strings, take the first digit of a, 1, and take the first digit of b, 2; obviously, 2 is greater than 1. So it returns normal.

Solution:

1. eval() function

if(eval(a)<eval(b)) {   //逻辑业务 }
   eval()函数用于在不引用任何特定对象的情况下计算代码字符串。

2. parseINt() function

if(parseInt(a)<parseInt(b)) {   //逻辑业务 }
   parseInt()函数用于在转换为int。

3. Multiplication operation (disguised conversion type) (A primary school physical education teacher once said: Multiply both sides by the same number at the same time, and the equation remains unchanged)

if(a *10 < b * 10){
//逻辑业务
}

Related recommendations:

JS Compare the size of two values Example

The above is the detailed content of How to determine the size of two numerical values ​​in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn