javascript - Select2 在网站上无法正常工作

标签 javascript jquery css twitter-bootstrap jquery-select2

不太确定这是怎么回事,因为我相信我是按照 Select2 网站上的说法复制的。我也相信我已经通过 CDN 正确加载了它需要的必要文件。我知道 Select2 函数的调用有问题,因为我把它完全去掉了,它看起来仍然一样,尽管缺少所有的 select2 类代码。我觉得我已经把所有东西都拿出来了,并尽我所能改变了一切,但我仍然没有让它发挥作用。我真的认为它会像复制 CDN 链接一样简单,并像 Select2 网站上的示例那样调用函数,但我的网站似乎没有正确接受代码。我将附上我在我的网站上看到的图片以及下面的代码。我还将附上一张它应该是什么样子的图片。提前致谢!

<?php
/*==================================================================================
README:

This global var (`root_dir`) must be set to the relative path
of the root directory.

E.g.
 If this page is in "career.clemson.edu/coop/"
 then the line should read
    $GLOBALS["root_dir"]="../";
 or if this page is in "career.clemson.edu/coop/students/";
 then the line should read
    $GLOBALS["root_dir"]="../../";


The template automatically includes common meta elemtents (keywords, etc),
common CSS files (ours, bootstrap, font-awesome), a fallback fon non-HTML5 compliant
browsers, and some basic javasript files at the end of the page.
Feel free to add any other things as needed on a per page basis!

All includes *should* in %root_dir%/includes


==================================================================================*/
    $GLOBALS["root_dir"]="../";
    require_once $GLOBALS["root_dir"].'includes.inc';
?>
<!doctype html>
<html lang="en">
<html><head>
<meta charset="UTF-8">
    <?php
        require_once $GLOBALS['include_loc']."common_meta.inc";
        require_once $GLOBALS['include_loc']."common_css.inc"; 
        require_once $GLOBALS['include_loc']."html5_shiv.inc";
    ?>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">

  <title>Resumes and Cover Letters</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  <!-- jQuery library -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- Latest compiled JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>


</head>

<body>
    <?php require_once $GLOBALS["include_loc"]."top_menu.inc";?> <!-- Top Menu -->
    <div class span="row">
    <script type="text/javascript">
        $(".js-example-placeholder-multiple").select2({
        placeholder: "Select a state"
        });
    </script>
    <select class="js-example-placeholder-multiple">
        <option value="AL">Alabama</option>
        ...
        <option value="WY">Wyoming</option>
    </select>
        </div>



    <?php require_once $GLOBALS["include_loc"]."footer.inc";?>  <!-- Footer -->
    <?php require_once $GLOBALS['include_loc']."basic_js.inc"; ?>   <!--Include JS Files at the end for speed-->
</body>
</html>

Screen Shot

What it should look like

Small on left side

最佳答案

您需要在 HTML 元素之后移动您的 js(否则 js 无法找到该元素(尚不存在))。

还有:

  1. 您应该将您的 js 库移动到 </body> 之前标签

  2. 使用 $( document ).ready(function() { ... });) to wrap your js code, js should run only after DOM is ready.

.centered {
  text-align: center;
}
<!doctype html>
<html lang="en">
<html>

<head>
  <meta charset="UTF-8">
  <?php
        require_once $GLOBALS['include_loc']."common_meta.inc";
        require_once $GLOBALS['include_loc']."common_css.inc"; 
        require_once $GLOBALS['include_loc']."html5_shiv.inc";
    ?>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">

    <title>Resumes and Cover Letters</title>



</head>

<body>
  <?php require_once $GLOBALS["include_loc"]."top_menu.inc";?>
  <!-- Top Menu -->
  <div class="centered" span="row">
    <select class="js-example-placeholder-multiple">
        <option value="AL">Alabama</option>
        ...
        <option value="WY">Wyoming</option>
    </select>
  </div>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <!-- jQuery library -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <!-- Latest compiled JavaScript -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
  <script type="text/javascript">
  $( document ).ready(function() {
      $(".js-example-placeholder-multiple").select2({
        placeholder: "Select a state",
        width: 150
      });
  });
  </script>

  <?php require_once $GLOBALS["include_loc"]."footer.inc";?>
  <!-- Footer -->
  <?php require_once $GLOBALS['include_loc']."basic_js.inc"; ?>
  <!--Include JS Files at the end for speed-->
</body>

</html>

关于javascript - Select2 在网站上无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45888519/

相关文章:

javascript - AngularJS + Bootstrap - 验证文本框 - 如何仅在用户输入一些数据后显示验证反馈?

javascript - jquery 在执行 .hide() 和 .fadeIn() 方法之前闪烁 Div 元素

html - 菜单问题,没有出现在手机上

html - css 向下滚动的背景

javascript - HighCharts - 为只有单个数据点的系列启用标记?

javascript - 媒体查询与 $(window).resize() 冲突

jquery - IE8 - 如何在内容加载后运行 jquery 代码?

css - 如何使用 css 制作固定的 div 元素?

javascript - 为什么 exponentpush token 为空

jquery - 使用新的 jQuery 1.6 $.animate "promise"和回调之间的区别?