Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在网站上选择2无法正常工作_Javascript_Jquery_Css_Twitter Bootstrap_Jquery Select2 - Fatal编程技术网

Javascript 在网站上选择2无法正常工作

Javascript 在网站上选择2无法正常工作,javascript,jquery,css,twitter-bootstrap,jquery-select2,Javascript,Jquery,Css,Twitter Bootstrap,Jquery Select2,我不太清楚这是怎么回事,因为我相信我是按照Select2网站的说法复制的。我还相信我已经通过CDN正确加载了它需要的必要文件。我知道Select2函数的调用有问题,因为我把它完全取出来了,尽管缺少所有Select2类代码,但它看起来还是一样。我觉得我已经尽我所能把所有的东西都拿走了,改变了所有的东西,但我仍然无法让它发挥作用。我真的认为这就像复制CDN链接一样简单,并像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>

简历和求职信
$(“.js示例占位符多”)。选择2({
占位符:“选择一个状态”
});
阿拉巴马州
...
怀俄明州


您需要在HTML元素之后移动js(否则js找不到元素(还不存在))

此外:

  • 您应该在
    标记之前移动js库

  • 使用
    $(document.ready(function(){…});)要包装js代码,js应仅在DOM就绪后运行。

  • .centered{
    文本对齐:居中;
    }
    
    简历和求职信
    阿拉巴马州
    ...
    怀俄明州
    $(文档).ready(函数(){
    $(“.js示例占位符多”)。选择2({
    占位符:“选择一个状态”,
    宽度:150
    });
    });
    
    非常感谢您!这就是问题所在,我不敢相信这就是问题的原因…@BradleyNewman尝试使用
    $(document.ready(function(){…});)
    包装您的js代码,这样js只会在DOM就绪(html等)后运行。您是否碰巧也知道为什么它只占据屏幕左侧的一小部分?我怎样才能使它更大?@BradleyNewman Cool,请接受答案,只要你能=D好运编码。我应该在标记之前包装脚本src还是脚本本身?