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

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

小云云
Release: 2023-03-22 14:38:01
Original
2685 people have browsed it

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"
};
Copy after login

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

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

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()函数用于在不引用任何特定对象的情况下计算代码字符串。
Copy after login

2. parseINt() function

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

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){
//逻辑业务
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template