Home  >  Article  >  Web Front-end  >  Can jquery modify the value of input?

Can jquery modify the value of input?

WBOY
WBOYOriginal
2022-06-09 16:53:463200browse

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");".

Can jquery modify the value of input?

The operating environment of this tutorial: windows10 system, jquery3.4.1 version, Dell G3 computer.

Can jquery modify the value 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()

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:

Can jquery modify the value of input?

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!

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