IE8中的JQuery.filter()
IE8似乎不支持JqueryIE8中的JQuery.filter(),jquery,arrays,internet-explorer-8,filter,Jquery,Arrays,Internet Explorer 8,Filter,IE8似乎不支持Jquery.filter()方法- 我有下面的代码过滤下拉列表 if($('#deliveryPostcodeEstimator').length > 0) { $('.shippingregionselector').hide(); $('#deliveryPostcodeEstimator') .blur(function() { //Set to default
.filter()
方法-
我有下面的代码过滤下拉列表
if($('#deliveryPostcodeEstimator').length > 0) {
$('.shippingregionselector').hide();
$('#deliveryPostcodeEstimator')
.blur(function() {
//Set to default
$('select[name=country] option:last').prop('selected', true);
//var defaultPostcode = 'GL50';
//$("select[name=country] option").filter(function() {
// return $(this).text() == defaultPostcode;
//}).prop('selected', true);
//Set to matching postcode value if set
$('select[name=country] option').filter(function(index) {
return ($(this).text() == $('#deliveryPostcodeEstimator').val().toUpperCase().substring(0,4).trim())
}).prop('selected', true);
//Submit
var thisForm = $(this).closest("form");
thisForm.submit();
})
.keyup(function() {
$(this).val($(this).val().toUpperCase());
});
$('button.pcodechange').click(function() {
var thisForm = $(this).closest("form");
thisForm.submit();
});
}
问题是
return ($(this).text() == $('#deliveryPostcodeEstimator').val().toUpperCase().substring(0,4).trim())
这会产生以下错误
对象不支持此属性或方法
如何像前一篇文章中建议的那样“将其包装在对象中”
谢谢您的错误可能是由于调用了
.trim()
造成的
根据下表,String.prototype.trim
不适用于Internet Explorer 8:
您可以改用jQuery.trim:
var search = $.trim($('#deliveryPostcodeEstimator').val().toUpperCase().substring(0,4));
$('select[name=country] option').filter(function(index) {
return $(this).text() == search;
}).prop('selected', true);
您还可以使用Cernunos链接中描述的纯javascript解决方案:
IE9及以后的支持。过滤器() 以下是您可以做的: 定义自己的过滤器
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun /*, thisp */) {
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in t) {
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
if(!Array.prototype.filter){
Array.prototype.filter=函数(fun/*,thisp*/){
“严格使用”;
if(this==void 0 | | this==null)
抛出新的TypeError();
var t=对象(本);
var len=t.length>>>0;
如果(乐趣的类型!=“功能”)
抛出新的TypeError();
var-res=[];
var thisp=参数[1];
对于(变量i=0;i
jQuery.filter和array.filter是不同的东西看起来IE8不支持jQuery.filter()方法
您在浏览器调试工具的控制台中是否收到错误或任何警告消息?你也在Chromer/FireFox中运行过吗?你有没有试着做一个检查,看看它是否都在那里工作?好的。因此,也许我的问题应该更多地围绕“如何在IE8中实现这一点?”这在Chrome/FFS中很好错误是“对象不支持此属性或方法”您使用的是哪个版本的jQuery?+1很好的发现。还指出,在MDN文档中,这似乎是正确的答案,这可能有助于解决问题