Jquery and native js get examples of values selected in the select drop down box
- 2020-03-26 21:37:44
- OfStack
Now I have a drop-down box with id=test, how do I get the selected value?
Use javascript native methods and jquery methods, respectively
One: javascript native methods
Var myselect= document.getelementbyid ("test");
2: get the index of the selected item: var index= myselect.selectedindex; // selectedIndex represents the index of the item you selected
3: value of selected options: myselect.options[index]. Value;
Text: myselect.options[index]. Text;
2. Jquery methods (provided that the jquery library has been loaded)
1: var options = $(" # test option: selected "); // gets the selected item
2: alert (options. Val ()); // gets the value of the selected item
3: alert (options. The text ()); // get the text of the selected item
Use javascript native methods and jquery methods, respectively
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
One: javascript native methods
Var myselect= document.getelementbyid ("test");
2: get the index of the selected item: var index= myselect.selectedindex; // selectedIndex represents the index of the item you selected
3: value of selected options: myselect.options[index]. Value;
Text: myselect.options[index]. Text;
2. Jquery methods (provided that the jquery library has been loaded)
1: var options = $(" # test option: selected "); // gets the selected item
2: alert (options. Val ()); // gets the value of the selected item
3: alert (options. The text ()); // get the text of the selected item