Get all the values of a dropdownlist to an array using Javascript
how can I get the values of dropdownlist to an a开发者_如何学JAVArray?
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);
}
精彩评论