牛骨文教育服务平台(让学习变的简单)
博文笔记

Bootstrap select样式

创建时间:2014-10-22 投稿人: 浏览次数:40724

A custom select for @twitter Bootstrap using button dropdown.

Usage

Create your <select> with the .selectpicker class.

  1. <select class="selectpicker">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Enable Bootstrap-Select via JavaScript:

  1. $(".selectpicker").selectpicker();

Options can be passed via data attributes or JavaScript.

  1. $(".selectpicker").selectpicker({
  2. style: "btn-info",
  3. size: 4
  4. });

Example

Make this:

Become this:

  1. <select class="selectpicker">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

It also works with option groups:

  1. <select class="selectpicker">
  2. <optgroup label="Picnic">
  3. <option>Mustard</option>
  4. <option>Ketchup</option>
  5. <option>Relish</option>
  6. </optgroup>
  7. <optgroup label="Camping">
  8. <option>Tent</option>
  9. <option>Flashlight</option>
  10. <option>Toilet Paper</option>
  11. </optgroup>
  12. </select>

And with multiple selects:

  1. <select class="selectpicker" multiple>
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>
(Note that you should give your selectpicker a name that ends with array syntax (example: <select name="named[]" class="selectpicker" multiple>) such that all items can be processed with php,... This is the same for <select>s withoutbootstrap-select.)

You can limit the number of elements you are allowed to select via the data-max-option attribute. It also works for option groups.

 
  1. <select class="selectpicker" multiple data-max-options="2">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>
  6.  
  7. <select class="selectpicker" multiple data-max-options="2">
  8. <optgroup label="Condiments" data-max-options="2">
  9. <option>Mustard</option>
  10. <option>Ketchup</option>
  11. <option>Relish</option>
  12. </optgroup>
  13. <optgroup label="Breads" data-max-options="2">
  14. <option>Plain</option>
  15. <option>Steamed</option>
  16. <option>Toasted</option>
  17. </optgroup>
  18. </select>

You can set different Bootstrap classes via the data-style attribute:

       
  1. <select class="selectpicker" data-style="btn-primary">
  2. ...
  3. </select>
  4.  
  5. <select class="selectpicker" data-style="btn-info">
  6. ...
  7. </select>
  8.  
  9. <select class="selectpicker" data-style="btn-success">
  10. ...
  11. </select>
  12.  
  13. <select class="selectpicker" data-style="btn-warning">
  14. ...
  15. </select>
  16.  
  17. <select class="selectpicker" data-style="btn-danger">
  18. ...
  19. </select>
  20.  
  21. <select class="selectpicker" data-style="btn-inverse">
  22. ...
  23. </select>

You can also use the title attribute as an alternative to display when the option is selected:

Using the title attribute on a multiple select will show the default prompt text when nothing is selected (note: this will not work on single select elements as they must have a selected value):

  1. <select class="selectpicker" multiple title="Choose one of the following...">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Using the data-selected-text-format attribute on a multiple select you can specify how the selection is displayed.

The supported values are:

  • values A comma delimted list of selected values. (default)
  • count If one item is selected, then the value is shown, if more than one is selected then the number of selected items is displayed, eg 2 of 6 selected
  • count > x Where X is the number of items selected when the display format changes from values to count
  1. <select class="selectpicker" multiple data-selected-text-format="count">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>
  1. <select class="selectpicker" multiple data-selected-text-format="count>3">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. <option>Onions</option>
  6. </select>

You can also show the tick icon on single select with the show-tick class:

  1. <select class="selectpicker show-tick">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

The bootstrap menu arrow can be added with the show-menu-arrow class:

  1. <select class="selectpicker show-menu-arrow">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Classes added to the options are transferred to the select:

  1. <select class="selectpicker">
  2. <option>Mustard</option>
  3. <option class="special">Ketchup</option>
  4. <option>Relish</option>
  5. </select>
  1. .special {
  2. font-style: italic;
  3. font-weight: bold !important;
  4. color:#bc0000 !important;
  5. background:#000;
  6. }

Apply .span* class to the selects to set the width.