javascript - 为什么 indexOf 在 Internet Explorer 中不起作用?

标签 javascript internet-explorer indexof

此函数在表单 onSubmit 期间执行,并且在 Firefox 和 Chrome 中工作正常,但在 IE 中不工作。我怀疑它是 indexOf,但我似乎找不到让它工作的方法。

function checkSuburbMatch(e) {

var theSuburb = document.getElementById('suburb').value;
var thePostcode = document.getElementById('postcode').value;

var arrayNeedle = theSuburb + " (" + thePostcode + ")";

if(suburbs.indexOf(arrayNeedle) != -1) {
    alert("Suburb and Postcode match!");
    return false;
} else {
    alert("Suburb and Postcode do not match!");
    return false;
}

}

最佳答案

IE 在Array 上根本没有这个方法,您可以自己添加,from MDC :

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

这会添加 .indexOf() 如果它丢失了(此时这意味着您在 IE<9 中),那么您可以使用它。至于为什么连IE8都没有呢?我帮不了你...

关于javascript - 为什么 indexOf 在 Internet Explorer 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697555/

相关文章:

javascript - 通过 iframe 嵌入 Facebook 帖子不起作用

javascript - 解释一下Arrowlets源码这部分

javascript - 如何从 MongoDB 对象数组的原型(prototype)中删除属性?

javascript - selection.modify() 是否有 IE 替代品?

c# - 如何使用 String.IndexOf 方法找到正确的值?

javascript - jQuery:单击 native 音频元素

internet-explorer - 如何将 javascript 变量作为参数传递给 vbscript 函数(在 HTA 的上下文中)?

css - Div 在 IE 中未正确对齐 - 在 Firefox/Chrome 中完美运行

javascript - indexOf 在 JavaScript 中不起作用

jquery - 通过 jQuery 从部分文本中删除冒号?