Home > Web Front-end > JS Tutorial > Usage examples of val() method in jQuery_jquery

Usage examples of val() method in jQuery_jquery

WBOY
Release: 2016-05-16 16:24:36
Original
1316 people have browsed it

The example in this article describes the usage of the val() method in jQuery. Share it with everyone for your reference. The specific analysis is as follows:

This method sets or gets the value attribute of the matching element.

Only elements with value attributes can use this method. For example, input elements can use this method, but div elements cannot.

Usage:

Usage 1:

When this method takes no parameters, it returns the value attribute of the first matching element. For example:

Copy code The code is as follows:
$("input").val()

The above code is to obtain the value attribute value of the first input element.
Example code:

Copy code The code is as follows:






val() function-Script Home







  •         






Usage 2:

When this method takes parameters, it sets the attribute values ​​of all matching elements. For example:

Copy code The code is as follows:
$("input").val("This is a test script")

The above code sets the value attribute value of all input elements to "This is a test script".
Example:

Copy code The code is as follows:






val() function-Script Home







  •         






I hope this article will be helpful to everyone’s jQuery programming.

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