The attributes and styles of jquery. Val ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.