Home  >  Article  >  Web Front-end  >  There are several ways to get the input value in jquery

There are several ways to get the input value in jquery

青灯夜游
青灯夜游Original
2022-06-13 18:02:4829900browse

Two ways to get it: 1. Use val() to get the input value, the syntax is "$("input").val()"; val() can return the value of the value attribute, and the input element displays The value is controlled through the value attribute, so the value attribute value is the input value. 2. Use attr() to return the value attribute value to obtain the input value, the syntax is "$("input").attr("value")".

There are several ways to get the input value in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.0 version, Dell G3 computer.

In HTML, the value displayed in the input input box is controlled by the value attribute; that is, getting the input value means getting the value of the value attribute in the input element.

Therefore, you can use the following two methods to obtain the input value in jquery:

  • Use val()

  • Use attr()

Method 1: Use val() to obtain the input value

The val() method can return the value attribute of the selected element value. Syntax:

$("input").val()

Example:



	
		
		
		<script>
			$(function () {
		            $("button").click(function () {
		               var value=$(&quot;input&quot;).val();
					   console.log(value);
		            })
		        })
		    </script>
	
	
		

There are several ways to get the input value in jquery

Method 2: Use attr() to get the input value

attr() can get the value of the specified attribute in the element; you only need to set the parameter to "value" to get the value of the value attribute.

$("input").attr("value")

Core implementation code:

<script>
	$(function () {
		$("button").click(function () {
		   var value=$(&quot;input&quot;).attr(&quot;value&quot;);
		   console.log(value);
		})
	})
</script>

There are several ways to get the input value in jquery

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

The above is the detailed content of There are several ways to get the input value 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