Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the way to get the value of jquery

Shulou Source: shulou.com Published: 2022-05-31 17:02:11 09月21日 Update

This article mainly explains "what is the way of jquery value". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the way of jquery value".

There are five ways to get values: 1, use "element object .text ()" to get the text content of the specified element; 2, use "element object .html ()" to get the contents of the element containing tags; 3, use "input box element .val ()" to get user input; 4, use "element object .attr (" attribute name ")" to get the value of the specified attribute.

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

There are many ways to get values in jquery, which will be introduced below.

1. There are two ways to get the content value of an element: text () or html ()

The text () method returns the text content of the selected element.

The html () method returns the content (innerHTML) of the selected element.

Example: use text () to get text content, and html () to get content containing tags

$(document) .ready (function () {$("button") .click (function () {console.log ($("p"). Text ()); console.log ($("p"). Html ()) });}); get the content of p element

This is a paragraph.

2. Get the input value of the input input box

The input value of the input input box is controlled by the value property. It can be obtained directly using the val () method, or by the attr ("value") method.

$(document) .ready (function () {$("button") .click (function () {console.log ($("input [type=text]"). Val ()); console.log ($("input [type=password]"). Attr ("value")) });}); get the content of the input element

User name:

Secret code:

3. Get the element attribute value

Element attribute values can be obtained in two ways:

Attr () method

Prop () method

Difference: Boolean attributes with values of true and false, such as checked, selected, and disabled, are recommended to use the prop () method, while other properties are recommended to use the attr () method.

Example 1: use attr () to get the value of a normal attribute

$(document) .ready (function () {$("button") .click (function () {console.log ($("input"). Attr ("name"));}) Get the value of the name attribute

User name:

Example 2: use prop () to get the value of a Boolean attribute

$(document) .ready (function () {$("button") .click (function () {console.log ($("input"). Prop ("disabled"));}) Get the value of the disabled attribute

Secret code:

Thank you for your reading, the above is the content of "what is the way to value jquery?" after the study of this article, I believe you have a deeper understanding of what is the way to value jquery, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Elements contents attributes methods methods inputs objects text users examples learning Boolean suggestions tags user names common can be passed a variety of that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux Microsoft macOS Shulou Tech Info