How to create an array from a select list's text using jQuery?
Hey guys i bet this is an easy question but cant seem to find any help online, i have a select List box and i would like to get each list text and split them up into an array of how big the list box is without the user selecting the value. Right now my list box makes random values so the list can go up to 100
Example
<select id="selectBox">
<option value="1">Sel开发者_运维百科ect One</option> <--- I want just the text to input into array
<option vaue="2">Select Two</option>
</select>
There is already that functionality in the HTML Dom
Here is a referencing link for the HTML Dom SelectElement Options http://www.w3schools.com/jsref/coll_select_options.asp
var myoptions = $("#mySelectElementid")[0].options;
from here you can do the following
myoptions.length;
myoptions[0].value;
myoptions[0].text;
Use the DOM to get at them:
var options = document.getElementById("selectBox");
var optArray = [];
for (var i = 0; i < options.length; i++) {
optArray.push(options[i].text);
}
// now optArray has all the text elements from the option tags.
精彩评论