> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/javascript/su-dung-select2-de-thay-the-select-boxes.md).

# \[SELECT2] Sử dụng Select2 để thay thế select boxes

Select2 là công cụ khá tốt để tùy biến select box, nó hỗ trợ việc tìm kiếm, tagging, lấy dữ liệu từ nguồn khác ... Để sử dụng Select2, bạn cần chèn file JavaScript và CSS của nó vào trang web của bạn. Bạn có thể tải chúng về từ đây <https://github.com/select2/select2/tags> , copy vào thư mục dist và include chúng vào thẻ \<head> trên trang HTML của bạn. <br>

```
<link href="path/to/select2.min.css" rel="stylesheet" /> <br>
<script src="path/to/select2.min.js"></script>
```

Hoặc có thể sử dụng qua CDN

```
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
```

Hoặc cũng có thể sử dụng nó như 1 gem với Ruby on Rails<https://github.com/argerim/select2-rails> \
Để khởi tạo một select2 cho riêng bạn :

```
<script type="text/javascript">
  $('select').select2();
</script>
```

#### Một số tính năng hay dùng với Select2 <a href="#mot-so-tinh-nang-hay-dung-voi-select2-0" id="mot-so-tinh-nang-hay-dung-voi-select2-0"></a>

#### Cơ bản <a href="#co-ban-1" id="co-ban-1"></a>

```
$(document).ready(function() {
  $(".js-example-basic-single").select2();
  // Thêm các tùy chọn của bạn vào đây.
});

<select class="js-example-basic-single">
  <option value="AL">Alabama</option>
    ...
  <option value="WY">Wyoming</option>
</select>
```

#### Placeholders <a href="#placeholders-2" id="placeholders-2"></a>

```
$(".js-example-placeholder-single").select2({
  placeholder: "Select a state",
  allowClear: true
});
```

#### Hiển thị nút xóa các giá trị đã chọn <a href="#hien-thi-nut-xoa-cac-gia-tri-da-chon-3" id="hien-thi-nut-xoa-cac-gia-tri-da-chon-3"></a>

```
$(".js-example-clear").select2({
  allowClear: true
});
```

#### Đọc dữ liệu từ một mảng <a href="#doc-du-lieu-tu-mot-mang-4" id="doc-du-lieu-tu-mot-mang-4"></a>

```
var data = [{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];

$(".js-example-data-array").select2({
  data: data
})
```

#### Đọc dữ liệu từ nguồn khác sử dụng AJAX <a href="#doc-du-lieu-tu-nguon-khac-su-dung-ajax-5" id="doc-du-lieu-tu-nguon-khac-su-dung-ajax-5"></a>

```
ajax: {
  url: "/contacts",
  dataType: "json",
  delay: 200,
  data: function (params) {
    return {
      search: params.term
    };
  },
  processResults: function (data) {
    return {
      results: data
    };
  },
  cache: true
}
```

#### Disabled mode <a href="#disabled-mode-6" id="disabled-mode-6"></a>

```
$(".js-example-disabled").prop("disabled", false);
```

#### Ẩn ô tìm kiếm <a href="#an-o-tim-kiem-7" id="an-o-tim-kiem-7"></a>

```
$(".js-example-basic-hide-search").select2({
  minimumResultsForSearch: Infinity
});
```

#### Bắt các sự kiện <a href="#bat-cac-su-kien-8" id="bat-cac-su-kien-8"></a>

```
var $eventSelect = $(".js-example-events");

$eventSelect.on("select2:open", function (e) { log("select2:open", e); });
$eventSelect.on("select2:close", function (e) { log("select2:close", e); });
$eventSelect.on("select2:select", function (e) { log("select2:select", e); });
$eventSelect.on("select2:unselect", function (e) { log("select2:unselect", e); });
```

#### Chế độ thẻ Tagging <a href="#che-do-the-tagging-9" id="che-do-the-tagging-9"></a>

```
$(".js-example-tags").select2({
  tags: true
})
```

#### Automatic tokenization <a href="#automatic-tokenization-10" id="automatic-tokenization-10"></a>

```
$(".js-example-tokenizer").select2({
  tags: true,
  tokenSeparators: [',', ' ']
})
```

#### Hỗ trợ đã ngôn ngữ <a href="#ho-tro-da-ngon-ngu-11" id="ho-tro-da-ngon-ngu-11"></a>

```
$(".js-example-language").select2({
  language: "es"
});
```

#### Căn lề về bên phải <a href="#can-le-ve-ben-phai-12" id="can-le-ve-ben-phai-12"></a>

```
$(".js-example-rtl").select2({
  dir: "rtl"
});
```

#### Còn rất nhiều khả năng mà Select2 có thể làm, bạn có thể tham khảo thêm tại <https://select2.github.io/> <a href="#con-rat-nhieu-kha-nang-ma-select2-co-the-lam-ban-co-the-tham-khao-them-tai-httpsselect2githubio-13" id="con-rat-nhieu-kha-nang-ma-select2-co-the-lam-ban-co-the-tham-khao-them-tai-httpsselect2githubio-13"></a>

#### Nguồn <a href="#nguon-14" id="nguon-14"></a>

<https://select2.github.io/>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/su-dung-select2-de-thay-the-select-boxes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
