3

看起来 IE8 不支持 Jquery.filter()方法 -为什么 .filter() 在 Internet Explorer 8 中不起作用?

我有以下代码过滤下拉列表

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()) 

这给出了以下错误

Object doesn't support this property or method 如何按照上一篇文章中的建议“将其包装在一个对象中”?

谢谢

4

2 回答 2

5

你的错误可能是因为你的.trim()电话。

String.prototype.trim根据此表不适用于 Internet Explorer 8:http: //kangax.github.com/es5-compat-table/

你可以使用 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);

您还可以使用 cernunnos 链接中描述的纯 javascript 解决方案:

JavaScript 中的 .trim() 在 IE 中不起作用

于 2013-03-08T11:07:01.037 回答
0

IE9 及更高版本支持 .filter()

您可以执行以下操作: 定义您自己的过滤器。

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;
};
}
于 2014-06-03T11:34:29.610 回答