最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create an array from a select list's text using jQuery? - Stack Overflow

programmeradmin1浏览0评论

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">Select One</option>    <--- I want just the text to input into array
   <option vaue="2">Select Two</option>

</select>

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">Select One</option>    <--- I want just the text to input into array
   <option vaue="2">Select Two</option>

</select>
Share Improve this question edited May 20, 2010 at 19:04 Silent asked May 20, 2010 at 18:50 SilentSilent 6811 gold badge16 silver badges34 bronze badges 1
  • Could you give an example with some code? I'm not sure what you mean when you say "list text" and "how big the list box is". Split them up by what? – Kerry Jones Commented May 20, 2010 at 18:54
Add a ment  | 

2 Answers 2

Reset to default 4

There is already that functionality in the HTML Dom

Here is a referencing link for the HTML Dom SelectElement Options http://www.w3schools./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.
发布评论

评论列表(0)

  1. 暂无评论