在附加的选择字段上使用select2时,超出了最大调用堆栈大小

时间:2016-12-20 03:39:52

标签: javascript jquery select2

我附加了包含选择输入的div,但是当我尝试初始化select2时,我收到此错误,我的页面没有响应,直到我强行完成它。

$('.page-content').on('DOMNodeInserted', '.material-container', function () {
    var selectField = $(this).find(".select-material");
    selectField.select2();
});

以下是控制台错误。

jquery.js?27d9:5946 Uncaught RangeError: Maximum call stack size exceeded.
at HTMLSelectElement.eval (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:5946:21)
at domManip (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:5759:14)
at jQuery.fn.init.after (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:5944:10)
at jQuery.fn.init.jQuery.fn.(anonymous function) [as insertAfter] (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:6052:37)
at Select2._placeContainer (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5137:16)
at new Select2 (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5063:10)
at HTMLSelectElement.eval (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5667:26)
at Function.each (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:368:19)
at jQuery.fn.init.each (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:157:17)
at jQuery.fn.init.$.fn.select2 (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5664:14)
at HTMLDivElement.eval (eval at <anonymous> (http://crm.dev:3000/js/app.js:149:1), <anonymous>:51:17)

1 个答案:

答案 0 :(得分:1)

当你初始化一个select2实例时,大量的东西被添加到DOM中,DOMNodeInserted事件被触发然后你有一个无限循环。

我有2种解决方法(当然还有更多):