在附加的 select 字段上使用 select2 时超出了最大调用堆栈大小
Maximum call stack size exceeded when using select2 on appended select fields
我正在附加一个包含 select 输入的 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)
当你初始化一个 select2 实例时,大量的东西被添加到 DOM,DOMNodeInserted 事件被触发然后你有一个无限循环。
我有两种解决方法(当然还有更多):
检查select2是否已经初始化
使用jQuery one() 而不是 on()
http://api.jquery.com/one/
我正在附加一个包含 select 输入的 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)
当你初始化一个 select2 实例时,大量的东西被添加到 DOM,DOMNodeInserted 事件被触发然后你有一个无限循环。
我有两种解决方法(当然还有更多):
检查select2是否已经初始化
使用jQuery one() 而不是 on() http://api.jquery.com/one/