SoFunction
Updated on 2025-02-28

Example of jquery and native js getting selected values ​​in the select drop-down box

Now there is a drop-down box with id=test. How do you get the selected value?

Use javascript native methods and jquery methods respectively
Copy the codeThe code is as follows:

<select name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>


1: JavaScript native method

1: Get the select object: var myselect=("test");

2: Get the index of the selected item: var index=; // selectedIndex represents the index of the item you selected

3: Get the value of the selected option: [index].value;

4: Get the text of the selected options: [index].text;

Two: jquery method (provided that the jquery library has been loaded)

1:var options=$("#test option:selected"); //Get selected items

2:alert(()); //Get the selected item's value

3:alert(()); //Get the selected text