jquery - 如何处理 "The select2(' destroy') 方法在不使用 Select2 的元素上调用”

标签 jquery angularjs ui-select2

我正在使用 ui-select2 并尝试通过单击编辑按钮打开新页面。我看到一些奇怪的问题。当我第一次打开新页面时,它打开得很好,但是当我取消它并再次打开同一页面时,通过单击编辑,它给了我以下错误:

select2.full.min.js:3 The select2('destroy') method was called on an element that is not using Select2
angular.js:13708 TypeError: Cannot read property 'destroy' of undefined
at n.a.fn.select2 (select2.full.min.js:3)
at HTMLSelectElement.<anonymous> (select2.js:175)
at HTMLSelectElement.dispatch (jQuery-2.1.4.min.js:3)
at HTMLSelectElement.r.handle (jQuery-2.1.4.min.js:3)
at Object.trigger (jQuery-2.1.4.min.js:3)
at n.triggerHandler (jQuery-2.1.4.min.js:3)
at Function.pa.fn.on.pa.cleanData (angular.js:1865)
at n.remove (jQuery-2.1.4.min.js:3)
at angular.js:5504
at Object.push (angular.js:5085)

通过阅读此消息,似乎我必须定义销毁方法,但我不知道如何在 Controller 中定义销毁方法以及如何调用取消按钮(取消方法)调用。 以下是选择的代码片段:

<select ui-select2 class="form-control input-lg" ng-model="cityId" ng-change="loadLocality()">
                                <option ng-repeat="city in cities|orderBy:'name'|filter:'ACTIVE':true" value="{{city.id}}">{{city.name}}</option>
                            </select>

最佳答案

对 select2 使用简单的检查,如下所示:

if ($('select').data('select2')) {
   $('select').select2('destroy');
 }

我在 GitHub Issue 中找到了代码 https://github.com/aldeed/meteor-autoform-select2/issues/44

关于jquery - 如何处理 "The select2(' destroy') 方法在不使用 Select2 的元素上调用”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38797207/

相关文章:

jquery - 如何使用 jquery 取消选择链接

javascript - AngularJS 单选按钮在加载时未检查

jquery - Select2 无限滚动组

javascript - 如何在 AngularJS 中遍历模型数据?

jquery - select2不显示从ajax收到的结果

javascript - jQuery 查找整数并附加小数

javascript - 如何使用jquery将div加载到html中

c# - "Incorrect Content-Type: "异常抛出 Angular mvc 6 应用程序

javascript - Angular JS - {{ }} 不起作用,但 data-ng-bind 显示范围

javascript - 错误: 'let' declarations can only be declared inside a block