Js drop-down box select how to get the value and text content
select the drop-down box in project development is often used, particularly in the application-level menu with broader aspects. However, for some beginners, how to get the drop-down frame of mind the value node option values and text content, still a little difficult.
The other did not say, and now I wrote a piece of code, simply explain how to obtain value and the value of the text (text), necessary to add a note, I hope to help the drop-down box are not familiar friend.
html code:
</HEAD> <BODY> Books Category: <select id="s1" > <Option value = " . 1 " > - training </ option> <Option value = " 2 " >-technical </ option> </select> </BODY> </HTML>
window.onload = function(){ // first drop-down box to obtain the node object; var SELECT = document.getElementById ( " S1 " ); // 1. How to get the currently selected value? : Var value = SELECT .Value; // 2. How to get all of the drop-down box option node object var options = SELECT .options; // NOTE: The options to obtain an array of objects // 3. How to get the value of the value of several option such as I want to get the first option of value, this may be:? Var value1 = Options [ 0 ] .Value; // 4. How to get the first option of several text ? for example, I want to get the first option of the text, it can be: var text1 = Options [ 0 ] .text; // 5. How to get the index of the currently selected option? var index = SELECT .selectedIndex; // 6. How do I get the text of the currently selected option? // from the second question, you get an array of options that we have all of the option // and from the fifth question, we get to the index of the currently selected option // So long as we are under the same options [index] target the method to obtain the currently selected option var selectedText = Options [index] .text; }
select the drop-down box in project development is often used, particularly in the application-level menu with broader aspects. However, for some beginners, how to get the drop-down frame of mind the value node option values and text content, still a little difficult.
The other did not say, and now I wrote a piece of code, simply explain how to obtain value and the value of the text (text), necessary to add a note, I hope to help the drop-down box are not familiar friend.
html code:
</HEAD> <BODY> Books Category: <select id="s1" > <Option value = " . 1 " > - training </ option> <Option value = " 2 " >-technical </ option> </select> </BODY> </HTML>
window.onload = function(){ // first drop-down box to obtain the node object; var SELECT = document.getElementById ( " S1 " ); // 1. How to get the currently selected value? : Var value = SELECT .Value; // 2. How to get all of the drop-down box option node object var options = SELECT .options; // NOTE: The options to obtain an array of objects // 3. How to get the value of the value of several option such as I want to get the first option of value, this may be:? Var value1 = Options [ 0 ] .Value; // 4. How to get the first option of several text ? for example, I want to get the first option of the text, it can be: var text1 = Options [ 0 ] .text; // 5. How to get the index of the currently selected option? var index = SELECT .selectedIndex; // 6. How do I get the text of the currently selected option? // from the second question, you get an array of options that we have all of the option // and from the fifth question, we get to the index of the currently selected option // So long as we are under the same options [index] target the method to obtain the currently selected option var selectedText = Options [index] .text; }