Home >Web Front-end >Front-end Q&A >Can jquery get the value of the tag?

Can jquery get the value of the tag?

WBOY
WBOYOriginal
2022-09-07 16:48:471741browse

jquery can get the value in the tag; you can use the val() method to get the value in the tag. This method can return or set the value attribute of the selected element. The syntax for returning the tag value is "$(selector ).val();", the syntax for setting the tag value is "$(selector).val(value);".

Can jquery get the value of the tag?

The operating environment of this article: Windows 10 system, jquery version 3.6.0, Dell G3 computer.

Can jquery get the value of the tag

In jquery, you can use the val() method to get the value of the tag. The val() method returns or sets the value attribute of the selected element.

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

When used to return a value:

$(selector).val()

This method returns the value of the value attribute of the first matching element.

When used to set a value:

$(selector).val(value)

This method sets the value of the value attribute of all matching elements.

Example:

Get the value of setting the input tag

<input class="form-control" type="text" id="username" name="username" 
placeholder="" value="初始值" onblur="keyblurfun()">
<input class="form-control" type="text" id="username" name="username"
 placeholder="" value="{{ user.get(&#39;userid&#39;,&#39;&#39;) }}">
<script>
    var username = $("#username").val();   // 获取 id 为 username 的标签的值。
    console.log(username);
    $("#username").val("hello world");     // 设置 id 为 username 的标签的值
    console.log($("#username").val());
</script>

Get the value of the set textarea tag

<textarea class="form-control" id="brief" name="brief" cols=10 rows=6 placeholder="">Java</textarea>
<textarea class="form-control" id="brief" name="brief" cols=10 rows=6 placeholder="">
{{study.get(&#39;brief&#39;,&#39;&#39;)}}</textarea>
<script>
    var content= $("#brief").val();          // 获取 textarea 标签的值    
    console.log(content);            // Java
    $("#brief").val("Python");               // 设置 textarea 标签的值
    console.log($("#brief").val());  // Pyt<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>hon
</script>

Return the tag value example 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(){
alert($("input:text").val());
});
});
</script>
</head>
<body>
第一个名称: <input type="text" name="fname" value="Peter"><br>
最后一个名称: <input type="text" name="lname" value="Griffin"><br><br>
<button>返回第一个输入字段的值</button>
</body>
</html>

Output result:

Can jquery get the value of the tag?

After clicking the button:

Can jquery get the value of the tag?

Recommended related tutorials: jQuery video tutorial

The above is the detailed content of Can jquery get the value of the tag?. 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