Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

jquery - Uncaught TypeError: $(...).select2 is not a function

I download the select2 package and include the two files select2.min.css and select2.min.js into my laravel public file, select2.min.css into public/css folder and select2.min.js into public/js folder And I want to do a multiple select on my page. Here is my "select" like:

 <select class="form-control js-example-basic-multiple" name="tags"         
     multiple="multiple"> 
    @foreach($tags as $tag)
       <option value='{{ $tag->id }}'>{{ $tag->name }}</option>
    @endforeach
 </select>*

And this is my <script> section:

<script  src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="{{ URL::to('js/select2.min.js') }}"></script></script>
<script>
        $(document).ready(function(){
            $(".js-example-basic-multiple").select2();
        });
</script>

I don't know the multiple select is not working and browser always show "Uncaught TypeError: $(...).select2 is not a function"

Has everyone met this problem before?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most possible reason is to load jQuery twice. The second time when jQuery loaded it removes registered plugins.

Solution is

  1. Script loading should be in corrected order. That is

    1. jQuery,
    2. select2,
    3. your js files
    
  2. jQuery should be loaded only once.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...