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

发布于 2024-09-19 01:48:42 字数 506 浏览 3 评论 0原文

该函数在表单 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;
}

}

This function executes during the forms onSubmit, and works fine in Firefox and Chrome, but not in IE. I suspect it's indexOf, but I cannot seem to find a way to get it to work.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

全部不再 2024-09-26 01:48:43

IE 在 Array 上根本没有此方法,您可以添加它不过,您自己,来自 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都没有这个呢?我在那里帮不了你...

IE simply doesn't have this method on Array, you can add it yourself though, 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;
  };
}

This adds .indexOf() if it's missing (at this point that means you're in IE<9) then you can use it. As for why even IE8 doesn't have this already? I can't help you there...

懵少女 2024-09-26 01:48:43

如果您已经在项目中使用 jQuery,则可以使用 $.inArray()

http://api.jquery .com/jQuery.inArray/

If you are already using jQuery in your project you can use $.inArray()

http://api.jquery.com/jQuery.inArray/

林空鹿饮溪 2024-09-26 01:48:43

MSIE 11 和其他版本上的 indexOf() 不喜欢非字符串变量。在郊区添加 .toString() 应该可以修复它。

indexOf() on MSIE 11 and others it doesn't like non-string variables. On suburbs add .toString() and it should fix it.

谜泪 2024-09-26 01:48:43

当使用关联数组时这个函数很糟糕。

如果你将该函数放入你的代码中并执行此操作,

var a = new Array();

a["one"] = "1";

for(var i in a){

   alert(i)

}

你会得到 0, indexOf 这意味着你插入了 indexOf 作为你创建的每个数组的键

,但该数组应该只有一个关键是“一”

使用 jQuery!

-梅基亚斯

this function is bad when using associative arrays.

if you put that function in your code and do this

var a = new Array();

a["one"] = "1";

for(var i in a){

   alert(i)

}

You get 0, indexOf which means you inserted indexOf as a key to every array you create

but the array should only have one key and that is "one"

use jQuery!

-Mekias

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文