【问题标题】:Multiple calls to select2() on select input makes it disappear在选择输入上多次调用 select2() 使其消失
【发布时间】:2013-02-06 16:21:29
【问题描述】:

如何重现:

使用 Chrome 访问 http://ivaynberg.github.com/select2/select2-latest.html

打开网页开发控制台,输入:

$("#e1").select2()

"TypeError: Object -1 没有方法 'show'"

第一个选择消失了,为什么?

【问题讨论】:

    标签: javascript jquery jquery-select2


    【解决方案1】:

    问题在于,在第二次调用时,代码想要在创建新下拉列表之前销毁旧生成的下拉列表。它在正确初始化自身之前这样做。

    相关代码在第 645 行附近的“删除”函数中:

     if (select2 !== undefined) {
        select2.container.remove();
        select2.dropdown.remove();
        select2.opts.element
           .removeData("select2")
           .unbind(".select2")
           .attr("tabIndex", this.elementTabIndex)   <--- fail here
           .show(); 
    

    问题是它调用.attr("tabIndex", this.elementTabIndex)this.elementTabIndexundefined.attr(name, undefined) 相当于.attr(name),因此返回属性本身(而不是jquery 元素。所以你得到了属性值'-1' ,然后'-1'.show() 毫无意义,当然。

    作为一个组合,使用.attr("tabIndex", this.elementTabIndex || -1)。 更好的解决方法可能是仅在 this.elementTabIndex 初始化后调用 destroy。我不确定在destroy 中是否真的需要调用attr,但我不能声称我理解代码。

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 2020-06-30
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      相关资源
      最近更新 更多