<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> gets the value of the selected item for select </title>
<body>
<select id= "Select0" >
<option value= "0" > Please select options </option>
<option value= "1" > Option one </option>
<option value= "2" > Options two </option>
<option value= "3" > option three </option>
<option value= "4" > Options four </option>
<option value= "5" > Options five </option>
</select>
</body>
<script src= "Jquery-2.1.1.min.js" ></script>
<script>
Window.onload=function () {
var tags=document.getelementsbytagname ("select");
Use JavaScript to get the Select ""
var S=document.getelementbyid ("Select0");
s.selectedindex=2;//to set the index of the selected item
var index= s.selectedindex;//Gets the index of the selected item
Console.log (S.options[index].value);//Output The value of the selected item: value=3
Console.log (S.options[index].text);//Output The value of the selected item in option: Options three
You can also get the following values by using the following method
var Selectedvalue=document.getelementbyid ("Select0"). Value;
Console.log (SelectedValue);
Using the JQuery method to set and get
Console.log ($ ("#select0"). Find ("option:selected"). Text ());//Gets the text inside of the selected option
Console.log ($ ("#select0"). Val ());//Gets the selected value
$ ("#select0"). Val (1);//Set the selected value
Console.log ($ ("#select0"). Val ());
Combination of jquery and JavaScript
var sindex=$ ("#select0"). Get (0). SelectedIndex;
Select the item according to text= ' option four '
$ ("#select0 option[text= ' option One ']"). attr ("Selected", "true");
$ ("#select0 option[value= ' 2 ']"). Remove ();//delete value= ' 2 ' option to delete
Remove all other items except the first item
$ ("#select0 option[value!= ' 0 ']"). Remove ();//can be used to correlate multiple selection boxes, and then clear another option box when one of the options boxes starts to be selected.
};
</script>
This article is from the "Leefeng" blog, make sure to keep this source http://10173384.blog.51cto.com/10163384/1735070
Gets and sets the value of the selected item of select