Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the way to get the value of jquery

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report