【问题标题】:IndexOf not Supported in IE8 BrowserIE8 浏览器不支持 IndexOf
【发布时间】:2012-02-09 12:15:03
【问题描述】:

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

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

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

以下是代码:

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

}

谢谢你..

【问题讨论】:

  • 能否请您发布您的代码。
  • AFAIK,indexOf 在 ie7 和 8 中运行良好。请显示一些代码
  • @Archer 他说它在 ie9 中确实有效,而不是 7+8 :) 无论如何,它应该在所有这些中都有效
  • 数组的indexOf方法在IE
  • 正确 - 我收回了。但是,就像我所说的删除它留下相同的错误意味着这不是问题。

标签: javascript jquery internet-explorer telerik


【解决方案1】:

Arrays 的 indexOf() 方法在 IE $.inArray(),例如

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

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

【讨论】:

    【解决方案2】:

    documentation for indexOf on MDN 包含一个 pollyfill,它将在不支持 JavaScript 1.6 的浏览器中添加支持。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2019-07-30
      • 2012-02-14
      相关资源
      最近更新 更多