在Internet Explorer中获取选择框的选项 [英] Get options of a select box in Internet explorer

查看:122
本文介绍了在Internet Explorer中获取选择框的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试从html select元素中获取选项.我正在使用的逻辑在firefox中有效,但在IE中不起作用.它给了我options数组的长度或options的数量,但没有给我options的值.如何解决此问题?

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);}

推荐答案

以下代码应将值放入"vals"数组中.

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

这篇关于在Internet Explorer中获取选择框的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆