为什么 indexOf 在 Internet Explorer 中不起作用?
该函数在表单 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
IE 在 Array 上根本没有此方法,您可以添加它不过,您自己,来自 MDC:
这会添加
.indexOf ()
如果缺少(此时意味着您使用的是 IE<9),那么您可以使用它。至于为什么连IE8都没有这个呢?我在那里帮不了你...IE simply doesn't have this method on Array, you can add it yourself though, from MDC:
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...如果您已经在项目中使用 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/
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.
当使用关联数组时这个函数很糟糕。
如果你将该函数放入你的代码中并执行此操作,
你会得到 0,
indexOf
这意味着你插入了indexOf
作为你创建的每个数组的键,但该数组应该只有一个关键是“一”
使用 jQuery!
-梅基亚斯
this function is bad when using associative arrays.
if you put that function in your code and do this
You get 0,
indexOf
which means you insertedindexOf
as a key to every array you createbut the array should only have one key and that is "one"
use jQuery!
-Mekias