Home >Web Front-end >Front-end Q&A >How to find the value of the input box in jquery

How to find the value of the input box in jquery

青灯夜游
青灯夜游Original
2022-05-24 16:46:265961browse

Two methods to find the value of the input box: 1. Use val() to directly obtain the content value of the input box (input), the syntax is "$("input").val()"; 2. Use attr() to get the value of the value attribute in the input element, the syntax is "$("input").attr("value")".

How to find the value of the input box in jquery

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

In HTML, input boxes are defined using input tags. Therefore, finding the value of the input box is to find the value of the input tag.

The input tag value is controlled by the value attribute. Therefore, the last thing you need to ask for is the value of the value attribute of the input tag.

Method 1: Use val() to get the value

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

The value of the element is set through the value attribute. This method is mostly used for input elements.

If this method does not set parameters, it returns the current value of the selected element.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					console.log($("input").val());
				});
			});
		</script>
	</head>
	<body>

		<input type="text" name="user" value="Hello World" /><br><br>
		<button>获取输入框的值</button>

	</body>
</html>

How to find the value of the input box in jquery

Method 2: Use attr() to get the value

Above val The () method is specially used to set or return the content of the input tag, while attr() is used to operate attributes and can return the value of the specified attribute.

Just specify the attribute name whose value needs to be obtained.

$(document).ready(function() {
	$("button").click(function() {
		console.log($("input").attr("value"));
	});
});

How to find the value of the input box in jquery

[Recommended learning: jQuery video tutorial, web front-end video

The above is the detailed content of How to find the value of the input box in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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