Javascript IE8 浏览器不支持 IndexOf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8848097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:47:20  来源:igfitidea点击:

IndexOf not Supported in IE8 Browser

javascriptjqueryinternet-explorertelerik

提问by Dhaval Shukla

I have cascaded dropdown in my application, cascaded using jquery, now my problem is it is working smoothly with IE9, Firefox, Opera and Safari but does not work with any of browsers like IE7,IE8 etc.

我在我的应用程序中使用了级联下拉菜单,使用 jquery 级联,现在我的问题是它在 IE9、Firefox、Opera 和 Safari 上运行流畅,但不适用于 IE7、IE8 等任何浏览器。

I surfed for the problem and found that error is due to indexOf in my jquery code, i tried it by removing indexOf but still it is giving the same error..

我浏览了这个问题,发现错误是由于我的 jquery 代码中的 indexOf 引起的,我通过删除 indexOf 进行了尝试,但它仍然给出了相同的错误..

Note: Is there any work around in telerik script to remove indexOf, coz new only i can find indexOf in their script.

注意:在 Telerik 脚本中是否有任何解决方法可以删除 indexOf,因为只有我可以在他们的脚本中找到 indexOf。

Below is the Code:

下面是代码:

function OnClientSelectedIndexChanged(sender, eventArgs) {
var senderId = sender.get_id().toString();

var uniqueName = senderId.substring(senderId.lastIndexOf('_'), senderId.length);

if(senderId.indexOf("drpdwnCondition") > 0)
{
   return false;
}

var selectedItem = eventArgs.get_item();
var selectedValue = selectedItem.get_value().split('_');
$.ajax({ type: "POST", async: true,
    url: "/SalesRepresentativeMonitoring.aspx/GetData", contentType: "application/json; charset=utf-8",
    data: "{value:" + JSON.stringify(selectedValue[1]) + "}", dataType: "json",
    success: function (msg) {
        var resultAsJson = msg.d // your return result is JS array
        // Now you can loop over the array to get each object
        var cnditionCombo = $find("ctl00_ContentPlaceHolder1_radDock_C_Filter_drpdwnCondition" + uniqueName.toString());
        cnditionCombo.clearSelection();
        cnditionCombo.trackChanges();
        cnditionCombo.clearItems();
        for (var i in resultAsJson) {
            //alert(resultAsJson[i]);
            var item = new Telerik.Web.UI.RadComboBoxItem();
            item.set_text(resultAsJson[i]);
            item.set_value(resultAsJson[i]);
            cnditionCombo.get_items().add(item);
        }
        var itemAtIndex = cnditionCombo.get_items().getItem(0);  //get item in detailCB
        itemAtIndex.select();
        cnditionCombo.commitChanges();
    }
});

}

}

Thanking you..

感谢您..

回答by jabclab

The indexOf()method of Arrays is not implemented in IE < 9. As you're using jQuery you can make use of the $.inArray(), e.g.

s的indexOf()方法Array未在 IE < 9 中实现。当您使用 jQuery 时,您可以使用$.inArray(),例如

var arr = ["foo", "bar", "baz"],
    bazIndex = $.inArray("baz", arr), // 2
    doesntExistIndex = $.inArray("notThere", arr); // -1

Here's the documentation: http://api.jquery.com/jQuery.inArray/.

这是文档:http: //api.jquery.com/jQuery.inArray/

回答by Quentin

The documentation for indexOfon MDNincludes a pollyfill that will add support in browsers that don't support JavaScript 1.6.

MDN 上文档indexOf包括一个 pollyfill,它将在不支持 JavaScript 1.6 的浏览器中添加支持。

You can drop it in to avoid having to rewrite the existing code.

您可以将其放入以避免重写现有代码。

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}