0){...}" method to determine whether the variable is empty That’s it."/> 0){...}" method to determine whether the variable is empty That’s it.">

Home  >  Article  >  Web Front-end  >  How to determine if a variable is empty in jquery

How to determine if a variable is empty in jquery

藏色散人
藏色散人Original
2023-03-25 11:03:381429browse

Jquery method to determine whether a variable is empty: 1. Create a front-end sample file; 2. Pass "var A=$("#**).val();if(A==null| |A!=undefined||A==""){...}" or "var A=$("#**).val();if(A.length>0){...}" The method determines whether the variable is empty.

How to determine if a variable is empty in jquery

The operating environment of this tutorial: Windows 10 system, jquery3.2.1, Dell G3 computer.

How does jquery determine whether a variable is empty?

JQuery determines whether it is empty

//有如下三种判断
var A=$("#**).val();
if(A==null||A!=undefined||A==""){
//处理
}

Here is the difference between null, undefined and "":

null: empty object\not an object, converted to a numerical value It is 0

undefined: a special property of the global object window. undefined means "missing value", that is, there should be a value here, but it has not been defined yet. Convert the value to NaN. Typical usage is:

(1) When the variable is declared but not assigned a value, it is equal to undefined.

(2) When calling the function, the parameter that should be provided is not provided, and the parameter is equal to undefined.

(3) The object has no assigned attribute, and the value of this attribute is undefined.

(4) When the function does not return a value, it returns undefined by default.

There is another simpler method that has been tested and available

var A=$("#**).val();
if(A.length>0){
//非空处理
}

Recommended learning: "jQuery Video Tutorial"

The above is the detailed content of How to determine if a variable is empty 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