Home >Web Front-end >Front-end Q&A >Can jquery modify the value of input?
jquery can modify the value of the input by using the val() method; this method is used to set or return the value attribute of the specified element. The value attribute of the input is also the value of the element. You only need to set the parameters It can be the value of the modified input, and the syntax is "input element object.val("This is the changed value");".
The operating environment of this tutorial: windows10 system, jquery3.4.1 version, Dell G3 computer.
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()
Set value attribute:
$(selector).val(value)
Set value attribute through function:
$(selector).val(function(index,currentvalue))
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:text").val("这是改变后的值"); }); }); </script> </head> <body> <p>名称: <input type="text" name="user" value="这是初试值"></p> <button>设置输入字段的值</button> </body> </html>
Output result:
Video tutorial recommendation: jQuery video tutorial
The above is the detailed content of Can jquery modify the value of input?. For more information, please follow other related articles on the PHP Chinese website!