How to change the value attribute of input in jquery

WBOY
Release: 2022-09-09 16:02:14
Original
3602 people have browsed it

In jquery, you can use the val() method to change the value attribute of the input; the val() method is used to return or set the value attribute of the selected element, and the value attribute is used to specify the value of the input element. Syntax It is "$(input element object).val(input modified value attribute value);".

How to change the value attribute of input in jquery

The operating environment of this article: Windows 10 system, jquery version 3.6.1, Dell G3 computer.

How jquery changes the value attribute of input

The val() method returns or sets the value attribute of the selected element.

  • When used to return a value:

This method returns the value of the value attribute of the first matching element.

  • When used to set a value:

This method sets the value of the value attribute of all matching elements.

Note: The val() method is typically used with HTML form elements.

Syntax

Return value attribute:

$(selector).val()
Copy after login

Set value attribute:

$(selector).val(value)
Copy after login

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="js/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("input").val("PHP中文网");
});
});
</script>
</head>
<body>
<p>名称: <input type="text" name="user" value="www.php.cn"></p>
<button>设置input的值</button>
</body>
</html>
Copy after login

Output result:

How to change the value attribute of input in jquery

Recommended related tutorials: jQuery video tutorial

The above is the detailed content of How to change the value attribute of input in jquery. 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