Get the value and text of a drop-down list selection

Source: Internet
Author: User

JS Take value

Get the value of a drop-down list check

The code is as follows Copy Code
function Getselectedvalue (name) {
var Obj=document.getelementbyid (name);
return obj.value; So simple, you can get the Value property of its object directly
}

Get text value

The code is as follows Copy Code

<select id= "Select" >
<option>1</option>
<option selected= "Selected" >2</option>
<option>3</option>
</select>

<script language= "javascript" type= "Text/javascript" >
var Byid = document.getElementById ("select")
Alert (Byid.options[byid.selectedindex].text)
</script>

Example 2

The code is as follows Copy Code

<select id= "Select1" onchange= "ABC ()" >
<option value= "1" > Reading </option>
<option value= "2" > Sports </option>
</select>
<script type= "Text/javascript" >
Function abc () {
var obj =document.getelementbyid ("Select1");
alert (Obj.options[obj.selectedindex].text);
}
</script>

jquery procedure

The code is as follows Copy Code

$ ("#select option[selected]"). Text ();

There are spaces between select and option, and option is the child element of select

$ ("#select option:selected"). Text ();

If written as $ ("#select"). Text (), select all the Drop-down menu texts

Get value

The code is as follows Copy Code

$ ("#select"). Val ();//Fetch value
$ ("#select"). Val ("value");/setting, if there is an option in the select that has value values, the option is selected and if not, select does not

Any changes

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.