简体   繁体   中英

jQuery Datatables error (Uncaught TypeError: $ is not a function)

Im trying to get datatables to work for a basic table

This is my error

在此处输入图像描述

Below is my layouts file, where i include the script tags and css tags / they are noted

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <!-- font awesome icon kit - check favourite tabs in chrome to get more -->
    <script src="https://kit.fontawesome.com/69e69f8319.js" crossorigin="anonymous"></script>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{config('app.name', 'Coffee')}}</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <link rel="stylesheet" href="../css/app.css">

    <!-- ck editor cdn-->
    <script src="https://cdn.ckeditor.com/4.15.1/standard/ckeditor.js"></script>

    <!-- Datatables link -->
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.23/css/dataTables.bootstrap4.min.css">

</head>
<body style="background-color: lightgrey !important;">
@include('inc.admin_navbar')
<div class="container">
    @yield('content')

    @if(session('success_message'))
        <div class="alert alert-success">
            {{session('success_message')}}
        </div>
    @endif
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>

<!-- datatables js link -->
<script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.23/js/dataTables.bootstrap4.min.js"></script>

@include('sweetalert::alert')

</body>
</html>

Here is my file that includes the targeted table and the javascript. This file extends from the layouts file where the datatable links are nested

<table id="datatable" class="table table-striped table-hover">
        <htead>
            <th>ID</th>
            <th>Name</th>
            <th>Date Joined</th>
            <th></th>
        </htead>
        <tbody>
        @foreach($posts as $post)
            <tr>
                <th>{{$post->id}}</th>
                <th>{{$post->title}}</th>
                <th>{{$post->created_at}}</th>
                <th>
                    <div class="row">
                        <div class="col">
                            <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#editModal">Edit</button>
                        </div>
                        <div class="col">
                            <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#delete">Delete</button>
                        </div>
                    </div>
                </th>

            </tr>

        @endforeach
        </tbody>
    </table>

Here is the js at the bottom on the file

<script type="text/javascript">

    $(document).ready(function () {

        //linked to our main table - js function
        var table = $('#datatable').DataTable();


    })
</script>

please include Jquery before datatable load

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

Include jQuery into your code, right after body tag ends </body>

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM