如何将dropdownlist的值输入数组?
答案 0 :(得分:19)
var ddlArray= new Array();
var ddl = document.getElementById('ddl');
for (i = 0; i < ddl.options.length; i++) {
ddlArray[i] = ddl .options[i].value;
}
答案 1 :(得分:0)
在纯Javascript中,您可以遍历子节点并提取具有nodeName选项的任何节点。快速举例:
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]
答案 2 :(得分:0)
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);
}