我正在尝试使用Select2选择框,但在我的页面上不起作用。我的js技能很差,所以我尝试做最简单的例子。我正在使用引导程序库。整个代码如下:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8_polish_ci">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<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>
<link href="../inc/bipro.css" rel="stylesheet" type="text/css">
<title>Test</title>
</head>
<body>
<br>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
select2
</div>
<div class="card-body text-center">
<select class="aaaa form-control">
<option value="">wybierz</option>
<option value="1">banany</option>
<option value="2">cytryny</option>
<option value="3">jabłka</option>
</select>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$('.aaaa').select2();
});
</script>
</body>
</html>
我在做什么错
答案 0 :(得分:2)
您没有包括Bootstrap和Select2都需要的jQuery。
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
确保在Bootstrap和Select2之前导入jQuery
您缺少name=""
上的select
属性。我还要将该类重命名为有意义的名称,例如js-select
<select class="js-select form-control" name="fullnames">
<option value="0">wybierz</option>
<option value="1">banany</option>
<option value="2">cytryny</option>
<option value="3">jabłka</option>
</select>
然后您的脚本将如下所示:
<script type="text/javascript">
$(document).ready(function() {
$('.js-select').select2();
});
</script>