DIV CSS 佈局教程網

Bootstrap Select2 使用解析
編輯:關於JavaScript     

Select2 是 Bootstrap 專案中常見的下拉增強插件,支援搜尋、標籤、多選與 Ajax 載入。

固定資料

$(".js-example-tags").select2({
  tags: true,
  data: [
    { id: 0, text: "story" },
    { id: 1, text: "bug" },
    { id: 2, text: "task" }
  ]
});

Ajax 載入

$(".js-example-tags").select2({
  tags: true,
  ajax: {
    url: "Ask2/tags",
    processResults: function (data) {
      return { results: data };
    }
  }
});

使用 Ajax 時,後端回傳格式通常要是 id 與 text 的陣列,Select2 才能正確顯示與選擇。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved