@crestapps/bootstrap-select

中文文档
1.2.6发布于 2026/10/05MITESM + CJS零依赖构建来源已验证近 30 天下载 3079

A dependency-free, vanilla JavaScript plugin that brings select elements into the 21st century with intuitive multiselection, searching, and much more. Supports Bootstrap 5+. Forked from snapappointments/bootstrap-select with jQuery and Bootstrap 3/4 supp

原始使用文档
文档目录18 节
  1. About this fork
  2. Requirements
  3. Quick start
  4. Using the CDN build
  5. Package formats
  6. ESM
  7. CommonJS
  8. Browser global / UMD
  9. Usage
  10. Via the selectpicker class (automatic)
  11. Via JavaScript
  12. Calling methods
  13. Sanitizing custom HTML
  14. Events
  15. Documentation
  16. Building locally
  17. Bugs and feature requests
  18. Copyright and license

bootstrap-select (CrestApps fork)

A dependency-free, vanilla JavaScript plugin that brings select elements into the 21st century with intuitive multiselection, searching, and much more — for Bootstrap 5+.

License npm version Documentation

About this fork

This project is a fork of the original snapappointments/bootstrap-select.

The original bootstrap-select is an excellent, battle-tested plugin, but it depends on jQuery and has not seen active maintenance for some time. Because modern Bootstrap (v5+) dropped its jQuery dependency, and many projects are moving away from jQuery entirely, we decided to create this fork with the following goals:

  • Remove the jQuery dependency completely — the library is now written in plain, vanilla JavaScript and ships with no runtime dependencies (other than Bootstrap itself).
  • Support Bootstrap 5 and later only — older Bootstrap and jQuery compatibility paths are intentionally out of scope.
  • Ship modern distribution formats — the package includes first-class ESM, CommonJS, and browser-global UMD builds from the same source code.
  • Use a modern build pipeline — the fork no longer relies on Grunt or Less; builds are produced with Rollup, Sass, and the current docs/tooling stack.
  • Keep the select-enhancement feature set while prioritizing a modern, small, forward-only API.

Requirements

  • Bootstrap 5+ (CSS and JS, including its bundled Popper).

Quick start

Install with npm:

npm install @crestapps/bootstrap-select bootstrap

Load Bootstrap 5, then bootstrap-select's CSS and JS. Load bootstrap-select after Bootstrap's JavaScript when using the browser-global build.

<!-- Bootstrap 5 (includes Popper) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<!-- bootstrap-select -->
<link rel="stylesheet" href="dist/css/bootstrap-select.min.css">
<script src="dist/js/bootstrap-select.min.js"></script>

<!-- (Optional) translation files -->
<script src="dist/js/i18n/defaults-*.min.js"></script>

Using the CDN build

After the package is published to npm, it will also be available through jsDelivr. Prefer pinning an explicit package version in production:

<!-- Bootstrap 5 (includes Popper) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<!-- @crestapps/bootstrap-select from jsDelivr -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@crestapps/[email protected]/dist/css/bootstrap-select.min.css">
<script src="https://cdn.jsdelivr.net/npm/@crestapps/[email protected]/dist/js/bootstrap-select.min.js"></script>

You can replace @1.2.3 with the version you want to consume. During development, @latest also works, but a fixed version is safer for production deployments.

Package formats

The package now ships three first-class JavaScript entry styles from the same source code:

StylePackage entryDirect fileUse when
ESMimport Selectpicker from '@crestapps/bootstrap-select'dist/js/bootstrap-select.esm.mjsYou use native modules or a bundler that prefers ESM
CommonJSrequire('@crestapps/bootstrap-select')dist/js/bootstrap-select.cjsYou use a bundler or toolchain that still consumes CommonJS
Browser global / UMDn/adist/js/bootstrap-select.js or .min.jsYou load the plugin directly from a <script> tag or CDN

Important: bootstrap-select is still a browser plugin. The ESM and CommonJS builds are intended for browser bundles or browser-like runtimes with a DOM, not server-only Node.js execution.

ESM

Use the package import entry or the direct .mjs file:

import 'bootstrap/dist/css/bootstrap.min.css';
import '@crestapps/bootstrap-select/dist/css/bootstrap-select.css';
import Selectpicker, { Selectpicker as NamedSelectpicker } from '@crestapps/bootstrap-select';

const picker = new Selectpicker('#my-select', {
  liveSearch: true
});

console.log(Selectpicker === NamedSelectpicker); // true

The ESM build stays module-scoped; it does not add window.Selectpicker. If you prefer a direct file import, use @crestapps/bootstrap-select/dist/js/bootstrap-select.esm.mjs.

CommonJS

Use the package require() entry or the direct .cjs file:

require('bootstrap');
require('@crestapps/bootstrap-select/dist/css/bootstrap-select.css');

const Selectpicker = require('@crestapps/bootstrap-select');
// or: const { Selectpicker } = require('@crestapps/bootstrap-select');

const picker = new Selectpicker('#my-select', {
  liveSearch: true
});

The CommonJS entry exports the class as:

  • module.exports = Selectpicker
  • module.exports.Selectpicker = Selectpicker
  • module.exports.default = Selectpicker

Like the ESM build, the CommonJS build stays module-scoped and does not add window.Selectpicker. If you prefer a direct file require, use @crestapps/bootstrap-select/dist/js/bootstrap-select.cjs.

Browser global / UMD

When loaded via a <script> tag, the UMD build exposes a global window.Selectpicker / Selectpicker:

<!-- Bootstrap 5 (includes Popper) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

<!-- @crestapps/bootstrap-select from jsDelivr -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@crestapps/[email protected]/dist/css/bootstrap-select.min.css">
<script src="https://cdn.jsdelivr.net/npm/@crestapps/[email protected]/dist/js/bootstrap-select.min.js"></script>

<script>
  const picker = new Selectpicker('#my-select', {
    liveSearch: true
  });
</script>

Usage

Via the selectpicker class (automatic)

Add the selectpicker class to your <select> elements. They are automatically initialized once the DOM is ready — no JavaScript required.

<select class="selectpicker">
  <option>Mustard</option>
  <option>Ketchup</option>
  <option>Barbecue</option>
</select>

Existing bootstrap-select markup that uses title="..." placeholders or data-width="fit" is also supported:

<select class="selectpicker" title="Content Type" data-width="fit">
  <option>Article</option>
  <option>Blog Post</option>
</select>

Selection indicators

For single selects, use show-tick for the default checkmark, or set data-selection-indicator="checkbox" to render radio-style indicators automatically. On multiselects, data-selection-indicator="checkbox" renders a checkbox column.

<select class="selectpicker show-tick">
  <option>Article</option>
  <option selected>Blog Post</option>
</select>

<select class="selectpicker" data-selection-indicator="checkbox">
  <option>Article</option>
  <option selected>Blog Post</option>
</select>

<select class="selectpicker" multiple data-selection-indicator="checkbox">
  <option selected>Bootstrap 5</option>
  <option>Vue</option>
</select>

Via JavaScript

Initialize an instance with the Selectpicker class from your chosen module style. You can pass an element or a CSS selector string:

// Initialize a single select
const picker = new Selectpicker('#my-select', {
  liveSearch: true,
  size: 8
});

// Or reuse an existing instance / create one if needed
const picker2 = Selectpicker.getOrCreateInstance(document.querySelector('#my-select'));

To initialize several elements at once, loop over them:

document.querySelectorAll('.my-select').forEach(function (el) {
  new Selectpicker(el);
});

Calling methods

Methods are called directly on the instance:

const picker = Selectpicker.getInstance('#my-select');

picker.val('Cheese');     // set the value
picker.refresh();         // re-render after changing the underlying <select>
picker.selectAll();       // (multiple selects)
picker.destroy();         // remove the plugin and restore the original <select>

See Methods for the full list.

Sanitizing custom HTML

data-content HTML is sanitized by bootstrap-select's built-in zero-dependency sanitizer by default.

If you prefer a dedicated sanitizer such as DOMPurify, keep sanitize: true and provide a sanitizeFn. The hook receives an array of DOM nodes, so sanitize each node in place:

import DOMPurify from 'dompurify';

new Selectpicker('#my-select', {
  sanitize: true,
  sanitizeFn: function (domNodes) {
    domNodes.forEach(function (node) {
      node.innerHTML = DOMPurify.sanitize(node.innerHTML);
    });
  }
});

This keeps the library dependency-free by default while letting applications opt into a library-backed sanitizer when they need one.

Events

Events are dispatched as native CustomEvents on the original <select> element (named exactly like the original *.bs.select events) and can be listened for with addEventListener:

const select = document.querySelector('#my-select');

select.addEventListener('changed.bs.select', function (e) {
  // e.detail = { clickedIndex, isSelected, previousValue }
  console.log('value changed', e.detail);
});

select.addEventListener('show.bs.select', function (e) {
  // e.detail.bsEvent is the original Bootstrap dropdown event
  console.log('opening from', e.detail.bsEvent.relatedTarget);
});

select.addEventListener('loaded.bs.select', function () { /* ... */ });

Available events: loaded, rendered, refreshed, changed, show, shown, hide, hidden, maxReached, maxReachedGrp, fetched — each suffixed with .bs.select.

Documentation

Full documentation is hosted at bootstrap-select.crestapps.com, including Getting Started, Examples, Options, and Methods, and Events.

Building locally

To work with the package from source, install dependencies and build dist/:

# Requires Node.js 20.19 or newer for the development toolchain.
npm install
npm run build

Useful local workflows:

npm run lint
npm test

For documentation site commands and local Docusaurus setup, see docs/README.md.

Bugs and feature requests

Anyone and everyone is welcome to contribute. Please review the guidelines for contributing.

Copyright (C) 2012-2018 SnapAppointments, LLC (original work). Fork modifications Copyright (C) CrestApps.

Licensed under the MIT license.

版本与发布记录

@crestapps/bootstrap-select 共发布 11 个版本,首次发布于 2026/06/15;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.6latest2026/10/051 个月前
1.2.42026/08/122 个月前
1.2.32026/08/103 个月前
1.2.22026/08/103 个月前
1.2.12026/07/014 个月前
1.2.02026/06/174 个月前

关于 @crestapps/bootstrap-select 的常见问题

@crestapps/bootstrap-select 是什么?

A dependency-free, vanilla JavaScript plugin that brings select elements into the 21st century with intuitive multiselection, searching, and much more. Supports Bootstrap 5+. Forked from snapappointments/bootstrap-select with jQuery and Bootstrap 3/4 supp

如何安装 @crestapps/bootstrap-select?

在项目目录运行 npm install @crestapps/bootstrap-select,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:bootstrap(>=5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@crestapps/bootstrap-select 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/crestapps__bootstrap-select 是否存在。

@crestapps/bootstrap-select 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@crestapps/bootstrap-select 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.19.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@crestapps/bootstrap-select 可以商用吗?

@crestapps/bootstrap-select 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@crestapps/bootstrap-select 的下载量有多少?

根据 npm 官方下载统计,@crestapps/bootstrap-select 在 2026/10/11 – 2026/11/09 这 30 天内被下载 3,079 次,日均约 103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@crestapps/bootstrap-select 还在维护吗?

最新版本 1.2.6 发布于 2026/10/05(1 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 11 个;首次发布于 2026/06/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。