【发布时间】:2020-02-17 20:57:29
【问题描述】:
所以我正在尝试创建一个表单,用户可以在其中将一本书添加到数据库中。 我想使用 Select2 中的选择栏,它允许用户搜索选择元素。 我遵循了有关如何使这项工作的教程,但不知何故,它似乎对我不起作用。 关于我做错了什么有什么想法吗?
相关代码:
@extends('layout')
@section('title')
<title>Add new book</title>
@section('stylesheets')
<link href="css/select2.min.css" rel="stylesheet" />
<script src="js/select2.min.js"></script>
@endsection
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="card uper">
<div class="card-header">
Neues Buch hinzufügen
</div>
<div class="card-body">
<form method="post" action="{{ route('books.store') }}">
<div class="form-group">
@csrf
...
<div class="form-group">
<label for="authors">Author(en):</label>
<select name="authors[]" multiple class="form-control select2-multi <!-- @error('authors') is-invalid @enderror -->">
@foreach ($authors as $author)
<option value="{{ $author->id }}">{{ $author->name }}</option>
@endforeach
</select>
@error('authors')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
...
</form>
</div>
</div>
@endsection
<script type="text/javascript">
$('.select2-multi').select2();
</script>
【问题讨论】:
-
您是否尝试过使用选择元素的 id 而不是类
$('#MySelect').select2(); -
是的,这也不起作用
-
控制台有错误吗?
-
是,加载资源失败:服务器响应状态为 404(未找到) select2.min.css:1 加载资源失败:服务器响应状态为 404(未找到) ) create:16 Uncaught ReferenceError: $ is not defined at create:16 select2.min.css:1 无法加载资源:服务器响应状态为 404(未找到)
标签: jquery jquery-select2 laravel-6