【问题标题】:JQuery .filter() in IE8IE8 中的 JQuery .filter()
【发布时间】:2013-02-23 21:28:23
【问题描述】:

IE8 好像不支持 Jquery .filter() 方法 - Why won't .filter() work in 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 如何按照上一篇文章中的建议“将其包装在一个对象中”?

谢谢

【问题讨论】:

  • jQuery.filter 和 array.filter 是不同的东西
  • It looks like IE8 doesn't support the Jquery .filter() method 您是否在浏览器调试工具的控制台中收到错误或任何警告消息?您是否也在 Chromer/FireFox 中运行过它?您是否尝试过创建fiddle 来检查它是否在那里工作?
  • 好的。所以也许我的问题应该更像是“我怎样才能让它在 IE8 中工作?”在 Chrome/FF 中没问题
  • 错误是“对象不支持这个属性或方法”
  • 你用的是什么版本的jQuery?

标签: jquery arrays internet-explorer-8 filter


【解决方案1】:

您的错误可能是因为您的.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 解决方案:

.trim() in JavaScript not working in IE

【讨论】:

【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    相关资源
    最近更新 更多