简体   繁体   English

Select2未应用

[英]Select2 not being applied

I'm trying to use Select2 to replace a multi-select drop down on my site. 我正在尝试使用Select2替换我网站上的多选下拉列表。

The plan is that a user can select a list of countries by typing the country, which will then be auto-completed and stored in a tag-like structure, similar to how StackOverflow tags work. 该计划是,用户可以通过键入国家/地区来选择国家/地区列表,然后自动完成并存储在类似Tag的标签结构中,类似于StackOverflow标签的工作方式。

For some reason, nothing is happening when I call the select2 function in my code. 由于某种原因,当我在代码中调用select2函数时,什么也没有发生。

Below I've included my code and a link to a JSFiddle. 下面,我包括了我的代码和指向JSFiddle的链接。

Head: 头:

<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

JavaScript: JavaScript的:

$(document).ready(function() {

    // Countries to choose from

    var countries = [{
        id: 1,
        text: 'United Kingdom'
    }, {
        id: 2,
        text: 'United States of America'
    }, {
        id: 3,
        text: 'Germany'
    }, ];

    // Create countries dropdown
    $('#allowedCountries').select2({
        placeholder: 'Select allowed countries',
        data: countries,
        tags: true,
        tokenSeparators: [',', ' ']
    });
})

JSFiddle https://jsfiddle.net/3ms60wkw/ JSFiddle https://jsfiddle.net/3ms60wkw/

just call <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 只需调用<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
before 之前

     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

and it should work perfectly fine 它应该工作得很好

应该首先加载Jquery,因为select 2取决于JQuery的顶部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM