jquery select2的问题

时间:2016-11-08 07:45:05

标签: jquery select2

我正在第一次使用select2我正在尝试使用多个select2但它不能正常工作看看我的代码

         $(document).ready(function() {
           $("#e1").select2();
         });
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<select id="e1" style="width:300px" multiple="multiple">
  <option value="AL">Alabama</option>
  <option value="Am">Amalapuram</option>
  <option value="An">Anakapalli</option>
  <option value="Ak">Akkayapalem</option>
  <option value="WY">Wyoming</option>
</select>

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
$(document).ready(function() { 
                    $("#e1").select2();
                });
&#13;
<html>
    <head>        
        <script type="text/javascript" src="//code.jquery.com/jquery-2.1.3.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">

    </head>

    <body>
        <select id="e1" style="width:300px" multiple="multiple">
            <option value="AL">Alabama</option>
            <option value="Am">Amalapuram</option>
            <option value="An">Anakapalli</option>
            <option value="Ak">Akkayapalem</option>
            <option value="WY">Wyoming</option>
        </select>
    </body>
</html>
&#13;
&#13;
&#13;

您可能已包含的JS或版本问题, 检查我创造的小提琴。

https://jsfiddle.net/rev8vkcr/

检查我的HTML代码。

<html>
    <head>        
        <script type="text/javascript" src="//code.jquery.com/jquery-2.1.3.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">

        <script type='text/javascript'>
                $(document).ready(function() { 
                    $("#e1").select2();
                });
        </script>

    </head>

    <body>
        <select id="e1" style="width:300px" multiple="multiple">
            <option value="AL">Alabama</option>
            <option value="Am">Amalapuram</option>
            <option value="An">Anakapalli</option>
            <option value="Ak">Akkayapalem</option>
            <option value="WY">Wyoming</option>
        </select>
    </body>
</html>

答案 1 :(得分:0)

似乎你没有加载jQuery,或者其他一些脚本已经控制了全局$。与select2无关

根据您的代码,这应该是您的文件夹结构。 This should be your folder structre

  

dist文件夹必须包含两个文件夹js和css

     

js文件夹应包含select2.min.js

     

css文件夹应包含select2.min.css

     

外部js文件夹必须包含jQuery.js

这里myhtml.html代码就像

<html>

  <head>
    <link rel="stylesheet" href="dist/css/select2.min.css">
    <script src="js/jquery.js"></script>
    <script src="dist/js/select2.min.js"></script>
    <script>
      $(function(){
      $("#e1").select2();    
      });
    </script>

    <style>
      body {
        padding: 40px;
      }
    </style>
  </head>

 <body>
    <select id="e1" style="width: 300px" multiple="multiple">
        <option value="AK">Alaska</option>
        <option value="HI">Hawaii</option>
        <option value="CA">California</option>
        <option value="NV">Nevada</option>
        <option value="OR">Oregon</option>
        <option value="WA">Washington</option>
        <option value="AZ">Arizona</option>
        <option value="CO">Colorado</option>
        <option value="ID">Idaho</option>
        <option value="MT">Montana</option>
        <option value="NE">Nebraska</option>
        <option value="NM">New Mexico</option>
        <option value="ND">North Dakota</option>
        <option value="UT">Utah</option>
        <option value="WY">Wyoming</option>
    </select>
  </body>

</html>