js dynamic access to select the selected option

In a recent written report management module, need to go through the screening qualified data conditions, with the layout filter criteria have select, input and so on. Has acquired less than select the selected option in the debugging process. So you query some information, find a selected attribute can get to select the value of the option. Let's demonstrate through the demo:

2 ways:

A, jquery method (the page must be loaded through the jquery library) ------------------- recommended

1: var options = $ ( " # test option: selected"); // get the selected item
2: alert (options.val ()) ; // get the value of the selected item
3: alert (options.text () ); // get the text of the selected item

demo code:

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

Second, the method by native js

1: get select objects: var MySELECT = document.getElementById ( "the Test");
2: to get the index of the selected item: var index = myselect.selectedIndex; // selectedIndex represents your selected item index
3: Take options to the selected item of value: myselect.options [index] .Value;
. 4: get the options selected item text: myselect.options [index] .text;

 

Jqueiry recommend the first method to acquire select the selected option ~~~~~~~~

Published 80 original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/tscaxx/article/details/104229983