Select2() is not a function

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

So i have downloaded select2 i have "installed it" by putting it into my folder and then loaded it on my site when i check the console (where i can see all of the scripts being loaded) i can see the file select2.js

I went to their documentation and copied it and added $("#e9").select2();

However when i load the page i get the following error:

TypeError: $(...).select2 is not a function   $("#e9").select2(); 

Have anyone else experianced anything like this?

Additional information here is my script:

回答1:

I was having this problem when I started using select2 with XCrud. I solved it by disabling XCrud from loading JQuery, it was it a second time, and loading it below the body tag. So make sure JQuery isn't getting loaded twice on your page.



回答2:

This error raises if your js files where you have bounded the select2 with select box is loading before select2 js files. Please make sure files should be in this order like..

  • Jquery
  • select2 js
  • your js


回答3:

I was also facing same issue & notice that this error occurred because the selector on which I am using select2 did not exist or was not loaded.

So make sure that $("#selector") exists by doing

if ($("#selector").length > 0)    $("#selector").select2(); 


回答4:

Put config.assets.debug = false in config/environments/development.rb.



回答5:

The issue is quite old, but I'll put some small note as I spent couple of hours today investigating pretty same issue. After I loaded a part of code dynamically select2 couldn't work out on a new selectboxes with an error "$(...).select2 is not a function".

I found that in non-packed select2.js there is a line preventing it to reprocess the main function (in my 3.5.4 version it is in line 45):

if (window.Select2 !== undefined) {     return; } 

So I just commented it out there and started to use select2.js (instead of minified version).

//if (window.Select2 !== undefined) { //    return; //} 

And it started to work just fine, of course it now can do the processing several times loosing the performance, but I need it anyhow.

Hope this helps, Vladimir



回答6:

I used the jQuery slim version and got this error. By using the normal jQuery version the issue got resolved.



回答7:

you might be referring two jquery scripts which is giving the above error.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!