Home > Article > Web Front-end > There are several ways to get the input value in jquery
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")".
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=$("input").val(); console.log(value); }) }) </script>
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=$("input").attr("value"); console.log(value); }) }) </script>
[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!