无法获取未定义或空引用的属性“选项” [英] Unable to get property 'options' of undefined or null reference

查看:1230
本文介绍了无法获取未定义或空引用的属性“选项”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到上述错误即10请给我一个建议。工作正常

i am getting the above error in ie 10 please give me a suggestion.it is working fine in

function getSelectedIndex(element, value) {
    var selectedIndex = 0;
    for(i = 0; i < element.options.length; i++) {
        if(element.options[i].value == value) {
            selectedIndex = i;
            break;
        }
    }
    return selectedIndex;
}

element.options.length给出Unable获取undefined的属性'options'或null reference.please建议我一个示例代码。
编辑:当我使用兼容模式的IE11时它对我有用,但当我删除它并在正常模式下运行时,出现了上述问题。

element.options.length is giving Unable to get property 'options' of undefined or null reference.please suggest me a sample code. Edit : It was working for me when I was using IE11 with compatibility mode, but when I removed it and ran it in normal mode, the above issue occurred.

推荐答案

使用 elements.options.indexOf(value)(假设元素是定义的,它似乎不是)。顺便说一下,如果第一个元素匹配,则当前设计将返回零,完全没有匹配。如果你真的想编写自己的 indexOf 版本,最好在没有匹配的情况下返回 -1 确实如此。

Use elements.options.indexOf(value) (assuming element is defined, which it doesn't seem to be). By the way, your current design will return zero if the first element matches or there is no match at all. If you really want to write your own version of indexOf, better to return -1 in the case of no match as it does.

这篇关于无法获取未定义或空引用的属性“选项”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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