【问题标题】:Select2 Uncaught TypeError: Cannot read property 'call' of undefinedSelect2 Uncaught TypeError:无法读取未定义的属性“调用”
【发布时间】:2020-07-24 20:34:06
【问题描述】:

我正在尝试从 Select2 版本 4.0.7 升级到 4.0.13(或介于两者之间的任何版本)。当我这样做时,单击选择框时出现以下错误。

Uncaught TypeError: Cannot read property 'call' of undefined
    at r.option (select2.min.js:formatted:489)
    at r.append (select2.min.js:formatted:425)
    at d.<anonymous> (select2.min.js:formatted:530)
    at d.e.invoke (select2.min.js:formatted:289)
    at d.e.trigger (select2.min.js:formatted:283)
    at d.trigger (select2.min.js:formatted:3436)
    at select2.min.js:formatted:3353
    at n.query (select2.min.js:formatted:2138)
    at d.<anonymous> (select2.min.js:formatted:3352)
    at d.e.invoke (select2.min.js:formatted:289)

当我改回版本 4.0.7 时,一切都会按预期工作。这发生在站点上的所有 select2 框上,无论其初始化选项如何。完整版和普通版我都试过了。

我使用的是 jQuery 版本 3.5.1

【问题讨论】:

    标签: javascript jquery jquery-select2


    【解决方案1】:

    更新

    经过进一步检查,项目中有一些代码覆盖了 Window.Element。我修复了该代码,不再需要下面的 polyfill。

    原答案

    我不知道为什么,但我发现我需要添加一个 polyfill 才能使其工作。我正在使用 Chrome 版本 87.0.4280.141(官方构建)(64 位),但由于某种原因 Element.prototype.matches 返回未定义。我添加了以下Polyfill from MDN 来解决这个问题:

        //Select2 JS Polyfill
        if (!Element.prototype.matches) {
            Element.prototype.matches =
                Element.prototype.matchesSelector ||
                Element.prototype.mozMatchesSelector ||
                Element.prototype.msMatchesSelector ||
                Element.prototype.oMatchesSelector ||
                Element.prototype.webkitMatchesSelector ||
                function (s) {
                    var matches = (this.document || this.ownerDocument).querySelectorAll(s),
                        i = matches.length;
                    while (--i >= 0 && matches.item(i) !== this) { }
                    return i > -1;
                };
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-12
      • 2018-07-18
      • 2021-04-28
      • 2017-07-20
      • 1970-01-01
      • 2020-03-28
      • 2023-04-11
      • 2016-04-22
      相关资源
      最近更新 更多