javascript - IE 8 不支持 indexOf?

标签 javascript jquery html internet-explorer internet-explorer-8

<分区>

我需要根据 JSON 响应动态创建单选按钮。到目前为止,我在 Chrome 和 firefox 上所做的工作,但在 if(subItem[1].indexOf(",") >= 0) 上给出了 Object doesn't support this property or method

我的代码

$("#sList").live("change", function(){
    var currentService=this.value;
    var c1Svc=[];
    var c2Svc=[];

    if(absP.length==2)
    {
        $.each(compareServiceData,function(i,item){

            if(currentService==item[0])
            {
                var configCount=0;
                $.each(item[1],function(j,subItem){

                    var temp=subItem[1];

                    /*The JSON response contains List of Lists, so here if it contains a list it will be separated by "," so split it and store in a array, else directly store in a array*/
                    if(subItem[1].indexOf(",") >= 0)
                    {
                        var tList=temp.split(",");
                        $.each(tList,function(k,val){
                            if(configCount==0)
                            {
                                c1Svc.push(val);                                
                            }
                            else
                            {
                                c2Svc.push(val);                                
                            }
                        });
                    }
                    else
                    {
                        if(configCount==0)
                        {
                            c1Svc.push(subItem[1]);                             
                        }
                        else
                        {
                            c2Svc.push(subItem[1]);                             
                        }
                    }
                    configCount++;

                });
            }

        });

        if ($("#customServiceListing").length == 0)
        {               
            $("#compareContent").append('<table id="customServiceListing" align="center" width="90%" class="csm-table" border="1"><tbody><tr><td><form id="c1Service"></form></td><td><form id="c2Service"></form></td></tr></tbody></table>');
        }
        else
        {
            $('#c1Service').empty();
            $('#c2Service').empty();
        }

    }
    else
    {
        $("#compareContent").append('<table align="center" width="90%" class="csm-table" border="1"><tbody><tr><td><form><select id="c1Service"></select></form></td><td><select id="c2Service"></select></td><td><select id="c3Service"></select></td></tr></tbody></table>');
    }


    /*adding service radios to config1*/
    $.each(c1Svc,function(i,item){
        $("#c1Service").append('<input type="radio" name="customConfig1ServiceNames" id="'+item+'" value="'+i+1+'"/>'+item);
    });
    if(c1Svc.length>1)
        $("#c1Service").append('<br/>');

    /*adding service radios to config2*/
    $.each(c2Svc,function(i,item){
        $("#c2Service").append('<input type="radio" name="customConfig2ServiceNames" id="'+item+'" value="'+i+1+'"/>'+item);
    });
    if(c2Svc.length>1)
        $("#c2Service").append('<br/>');
});

更新

Here是IE8不支持的各种功能代码的List

更新 这里的问题是什么,对于它给我的每个值 -1 我正在使用 Sudhir 给出的代码

alert(subItem[1].indexOf(",")+", "+subItem[1]);

截图

enter image description here

更新

明白了,var temp=subItem[1].toString(); 是问题所在,将其转换为 String 有效。

最佳答案

IE 版本 < 9 没有 indexOf,因此您可以添加自己的:

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (elt /*, from*/) {
        var len = this.length >>> 0;
        var from = Number(arguments[1]) || 0;
        from = (from < 0) ? Math.ceil(from) : Math.floor(from);
        if (from < 0) from += len;

        for (; from < len; from++) {
            if (from in this && this[from] === elt) return from;
        }
        return -1;
    };
}

var subItem = [];
subItem[1]="CSMTestPWXListinerService,CSMTestPWXListinerService_ManualyAdded";
console.log(subItem[1].indexOf(","));
//returns 25 

关于javascript - IE 8 不支持 indexOf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9768574/

相关文章:

java - SOLR 会占用服务器大量内存吗?

javascript - .toFixed() 会导致计算错误吗?

javascript - 获取子指令中的范围值

javascript - JS - 让功能重复直到满足条件

javascript - 如何通过将鼠标悬停在另一个元素上来更改一个元素?

jquery - 如何检索标签的内容?

javascript - 显示与其他 div 的属性匹配的 div 值

html - using::before 在元素下添加阴影

javascript - html 移动导航,::after 仅在子菜单上是错误的

php - Linux 不允许下载带空格的文件名