You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel9中使用Larastarters时jQuery的$(document).ready与Select2失效求助

Laravel 9 + Larastarters 中 $(document).ready 与 Select2 失效问题

使用 laraveldaily/larastarters starterkit 的 Laravel 9 项目中遇到如下问题:

  • $(document).ready 函数及 Select2 组件无法正常运行
  • AJAX 请求、SweetAlert 组件,以及 AJAX 成功后通过 jQuery 修改元素值的功能可正常工作

失效代码示例

$(document).ready(function () {
    $('.js-example-basic-single').select2();

    $('#exampleModal').modal({backdrop: 'static', keyboard: false})

    $('#warehouse_id').select2({
        dropdownParent: $('#exampleModal'),
    });
});

可用代码示例

function edit(id){
    console.log('ID : ' + id);
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
        type: "GET",
        url: "{{ url('customers.getone') }}" + "/" + id,
        data: {  },
        dataType: 'json',
        success: function (res) {
            console.log(JSON.stringify(res));
            $('#id').val(res['id']);
            $('#id').attr('readonly',true);
            $('#nama').val(res['nama']);

            $('#cust_group_id').val(res['cust_group_id']).change();
            selectElement('cust_group_id', res['cust_group_id']);

            $('#warehouse_id').val(res['warehouse_id']).change();
            selectElement('warehouse_id', res['warehouse_id']);

            $('#exampleModalLabel').text('Edit Customer');
            $('#exampleModal').modal('show');
        },
        error: function(xhr, textStatus, error) {
            console.log('url : ' + this.url);
            console.log(xhr.responseText);
            console.log(xhr.statusText);
            console.log(textStatus);
            console.log(error);
            Swal.fire({
                icon: 'error',
                title: 'Error! ',
                text: xhr.statusText + ' ' + xhr.responseText + ' ' + textStatus + ' ' + error,
            })
        },
    });

已尝试操作

  • 按照官方步骤完成 laraveldaily/larastarters starterkit 的安装
  • 在 app.blade.php 文件中添加 jQuery CDN 引用

问题仍未解决,控制台报错:未捕获的引用错误:$ 未定义

内容的提问来源于stack exchange,提问作者Aucky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 23:20:46