Get options of a select box in Internet explorer

我只是一个虾纸丫 提交于 2019-12-25 02:58:08

问题


Hello I am trying to get the options from a html select element. The logic I am using is working in firefox, but it isn't working in IE. It gives me the length of the options array or the number of options but it isn't giving me the values of options. How do I troubleshoot this issue??

var SelectId= 'select_1'; //id of the html select element
options = document.getElementById(SelectId).options;
alert(options.length);
for(var o=0;o< options.length;o++)
{alert(options[o].value);}

回答1:


The following code should put the values into a "vals" array.

var sel = document.getElementById('select_1');
var vals = [];
for (var i = 0; i < sel.children.length; ++i) {
    var child = sel.children[i];
    if (child.tagName == 'OPTION') vals.push(child.value);
}
// vals now contains the values


来源:https://stackoverflow.com/questions/3231539/get-options-of-a-select-box-in-internet-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!