Home > Web Front-end > JS Tutorial > body text

How does jQuery get all values ​​of the same class tag (cannot be obtained by default)_jquery

WBOY
Release: 2016-05-16 16:35:24
Original
1133 people have browsed it

I happened to encounter this problem during development, because jQuery always only returns the value of the first class tag, so it cannot meet our requirements.

For example:

Copy code The code is as follows:

var btn = jQuery('.btn').val();

What is obtained is only the value of the first html element with the class tag btn.

To get the values ​​of all html elements of a set of class tags. You have to use jQuery's each traversal.

Copy code The code is as follows:

var btns = new Array(); //or written as: var btns= [];

jQuery('.btn').each(function(key,value){

btns[key] = $(this).val();

//Or you can also write:

// btns[key] = $(value).val();
});

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