One of the. Val () methods in jquery is primarily used to manipulate the values of form elements, such as input, select, and textarea.
The. Val () method
. val () No parameter, gets the current of the first element in the collection of matching elements
。 Val (value), sets the value of each element in the matching element collection
。 Val, a function to return the set value
Precautions:
. Val () handles the Select element, and when no selection is selected, he returns null
The. Val () method is used to set the value of the form's field
If the Selec element has a multip (multiple-selection) attribute, and at least one selection is selected, the. Val () method returns an array of the values for each selected item in the array guarantee.
. Summary of differences between HTML (),. Text (),. Val ()
. HTML (),. Text (), and. Val () Three methods are used to read the contents of the selected element: just. html () is the HTML content used to read the element (including HTML)
. HYML (htmlstring),. Text (texstring) and. Val (value) Three methods are used to replace the contents of the selection element, and if three methods are applied to multiple elements at the same time, the contents of all selection elements will be replaced.
. HTML (),. Text (),. Val () can use the return value of the callback function to dynamically change the contents of multiple elements.
eg
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Document</title>
<script src= "Http://libs.baidu.com/jquery/1.9.1/jquery.js" ></script>
<style>
p{
color:red;
margin:4px;
}
b{
Color:blue;
}
</style>
<body>
<select id= "single" >
<option> Baidu Network </option>
<option> Blog Park </option>
</select>
<select id= "multiple" multiple= "multiple" >
<option selected= "Selected" >imocc</option>
<option> Baidu Network </option>
<option selected= "selected" > Blog Park </option>
</select>
<input type= "Text" value= "click a Button"/>
<p></p>
<!--
<script>
A single select that returns the first
$ ("P"). Text ($ ("#single"). Val ());
</script>
<!--<script>
Multiple select selected, return ["Imocc", "Blog Park"]
$ ("P"). Text ($ ("#multiple"). Val ());
</script>
<!--<script>
Select a form to modify the value of
$ ("input[type= ' text ']"). Val (' Modify the form's Fields ');
</script>
</body>
The attributes and styles of jquery. Val ()