简体   繁体   中英

Get all the values of a dropdownlist to an array using Javascript

如何将dropdownlist的值获取到数组?

var ddlArray= new Array();
var ddl = document.getElementById('ddl');
for (i = 0; i < ddl.options.length; i++) {
   ddlArray[i] = ddl .options[i].value;
}

http://jsfiddle.net/2vtmP/

In pure Javascript you can iterate over the child nodes and pull out any nodes that have the nodeName option. Quick example:

var select = document.getElementById('whateverIdToYourSelect');

var arr = [];
for (var i = 0, l = select.childNodes.length; i < l; i++) {
    if (select.childNodes[i].nodeName === 'OPTION') arr.push(select.childNodes[i].innerHTML);
}
alert(arr) // [Contents,Of,Each,Option]
var sel = document.getElementById("yourSelectId");
var opts = sel.options;
var array = new Array();
for(i = 0; i < opts.length; i++)
{
    array.push(opts[i].value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM