我附加了包含选择输入的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)
答案 0 :(得分:1)
当你初始化一个select2实例时,大量的东西被添加到DOM中,DOMNodeInserted事件被触发然后你有一个无限循环。
我有2种解决方法(当然还有更多):
检查select2是否已初始化
Is there any way to check if an element has jquery select2 already applied to it?
使用jQuery one()而不是on() http://api.jquery.com/one/