Home > Web Front-end > JS Tutorial > How to use if in jquery to determine if the tag content is not empty

How to use if in jquery to determine if the tag content is not empty

coldplay.xixi
Release: 2023-01-04 09:37:44
Original
4225 people have browsed it

Jquery uses if to determine that the tag content is not empty: 1. When the tag has an id attribute set, use [$("#id")] to get the tag; 2. For tags that do not have an id set , use a similar jsoup expression to obtain, the code is [var nameVal = $("#aaa input[]].

How to use if in jquery to determine if the tag content is not empty

The operating environment of this tutorial: Windows7 system , jquery3.2.1 version, Dell G3 computer.

Recommendation: jquery video tutorial

##How to use if to determine that the tag content is not empty in jquery:

Problem description

It is necessary to determine whether the user selects an option in the list or enters custom content and whether it is not empty. If it is not empty, proceed to the next step. , otherwise prompt the user.

<p id="namep">姓名:
    <input type="text" class="name"></input>
</p>
Copy after login

Solution

Get the content of the specified tag and perform null processing

// $(document).ready(function(){ ... });
// 标签有设置 id 属性时,使用 $("#id") 获取标签即可
// 对于没有设置 id 的标签,使用类似jsoup表达式来获取
var nameVal = $("#aaa input[class=&#39;name&#39;]").val();
if(nameVal == null || nameVal == "" || nameVal == undefined){
    alert("name 输入框为空");
}
Copy after login

Remarks:

1. Obtain content - the difference between text(), html() and val()

  • text() - Set or return the selected element The text content

  • html() - Sets or returns the content of the selected element (including HTML tags)

  • val() - Set or return the value of the form field

2. If the label is added dynamically, you need to consider the undefined situation.

undefined means "missing value", that is, the specified tag should have a value, but it has not been defined. If the specified tag has not been generated in the html, its content is of course undefined, and the value is converted to NaN. The general situation is as follows:

(1) The variable is declared, but no value is assigned. When used directly, the variable value is 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 the attribute is undefined;

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

Related free learning recommendations: javascript learning tutorial

The above is the detailed content of How to use if in jquery to determine if the tag content is not empty. 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