此函数在表单 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/