Home >Web Front-end >JS Tutorial >jquery and native js get the value selected in the select drop-down box example_javascript skills

jquery and native js get the value selected in the select drop-down box example_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:18:441115browse

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

Use the native javascript method and the jquery method respectively

Copy the code The code is as follows:




1: JavaScript native method

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

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

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

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

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

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
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn