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

Laravel中app.scss导入bootstrap-select后selectpicker未定义问题求助

Fixing "jQuery.Deferred exception: $(...).selectpicker is not a function" in Laravel

Hey there, that error usually pops up because bootstrap-select isn't being loaded in the right order, or it can't access jQuery/Bootstrap properly. Let's walk through the correct setup step by step:

1. Double-check your dependency installation & load order

First, make sure you've installed all required packages via npm (if you haven't already):

npm install jquery bootstrap bootstrap-select

The critical rule here is load order: jQuery must come first, then Bootstrap JS, then bootstrap-select. Update your resources/js/bootstrap.js file to import them in this exact sequence:

// Load jQuery first and attach it to the window object
window.$ = window.jQuery = require('jquery');

// Next, load Bootstrap's core JavaScript
require('bootstrap');

// Finally, load bootstrap-select's functionality
require('bootstrap-select');

2. Import the bootstrap-select styles

In your resources/sass/app.scss, add the bootstrap-select CSS after Bootstrap's base styles to ensure proper styling inheritance:

// Bootstrap core styles
@import '~bootstrap/scss/bootstrap';

// bootstrap-select custom styles
@import '~bootstrap-select/dist/css/bootstrap-select.css';

3. Compile your assets

Run the appropriate Laravel Mix command to compile your CSS and JS files:

# For development (unminified, fast rebuilds)
npm run dev

# For production (minified, optimized)
npm run prod

4. Ensure your views load compiled assets correctly

In your Blade templates, include the compiled CSS in the <head> and JS right before the closing </body> tag. This ensures the DOM is fully loaded before you try to initialize selectpicker:

<head>
    <!-- Other meta tags and custom styles -->
    <link rel="stylesheet" href="{{ asset('css/app.css') }}">
</head>
<body>
    <!-- Your page content, including the select element -->
    <select class="selectpicker" data-live-search="true">
        <option>Option 1</option>
        <option>Option 2</option>
    </select>

    <!-- Load compiled JavaScript -->
    <script src="{{ asset('js/app.js') }}"></script>

    <!-- Initialize bootstrap-select -->
    <script>
        $(document).ready(function() {
            $('.selectpicker').selectpicker();
        });
    </script>
</body>

5. Troubleshooting common issues

  • Check the network tab: Open your browser's dev tools, go to the Network tab, and confirm app.css and app.js are loading without 404 errors.
  • Avoid duplicate jQuery: Make sure you aren't loading jQuery from a CDN and via npm—this can cause conflicts where the $ variable gets overwritten.
  • Verify Laravel Mix config: Ensure your webpack.mix.js has the basic setup to compile your assets:
    mix.js('resources/js/app.js', 'public/js')
       .sass('resources/sass/app.scss', 'public/css');
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:22:42