一、jQuery multiselect:
免费(根据WTFPL许可)
以不引人注目的方式工作
完全开源
键盘支持
提供一些回调
可通过CSS完全自定义
取决于jQuery 1.8+
微小的代码~8kb缩小
二、用法
HTML
<html>
<head>
<link href="path/to/multiselect.css" media="screen" rel="stylesheet" type="text/css">
</head>
<body>
<select multiple="multiple" id="my-select" name="my-select[]">
<option value='elem_1'>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4'>elem 4</option>
...
<option value='elem_100'>elem 100</option>
</select>
<script src="path/to/jquery.multi-select.js" type="text/javascript"></script>
</body>
</html>
$('#my-select').multiSelect()
Options
Name | type | default | description |
---|---|---|---|
afterInit | function | function(container){} | Function to call after the multiSelect initilization. |
afterSelect | function | function(values){} | Function to call after one item is selected. |
afterDeselect | function | function(values){} | Function to call after one item is deselected. |
selectableHeader | HTML/Text | null | Text or HTML to display in the selectable header. |
selectionHeader | HTML/Text | null | Text or HTML to display in the selection header. |
selectableFooter | HTML/Text | null | Text or HTML to display in the selectable footer. |
selectionFooter | HTML/Text | null | Text or HTML to display in the selection footer. |
disabledClass | String | ‘disabled’ | CSS class for disabled items. |
selectableOptgroup | Boolean | false | Click on optgroup will select all nested options when set to true. |
keepOrder | Boolean | false | The selected items will be displayed in the same order than they are selected. |
dblClick | Boolean | false | Replace the defautl click event to select items by the dblclick one. |
cssClass | String | “” | Add a custom CSS class to the multiselect container. |
.multiSelect(options): 以多选方式激活您的内容。 接受可选的选项对象
$('#your-select').multiSelect({});
注意:在调用以下方法之一之前,必须使用$(’#your-select’)。multiSelect()初始化多重选择。
.multiSelect(’select’,String | Array)
选择参数中给定值的项目。 该值可以是与选项oran值([‘elem_1’,’elem_42’])的值匹配的字符串(’elem_1’)。
$('#your-select').multiSelect('select', String|Array);
.multiSelect(’deselect’,String | Array)
使用参数中给定的值取消选择该项目。 该值可以是与选项oran值([‘elem_1’,’elem_42’])的值匹配的字符串(’elem_1’)。
$('#your-select').multiSelect('deselect', String|Array);
.multiSelect(‘deselect’)
取消选择之前选择的所有项目。
$('#your-select').multiSelect('select_all');
.multiSelect( ‘SELECT_ALL’)
选择所有元素。
$('#your-select').multiSelect('deselect_all');
.multiSelect(‘refresh’)
刷新当前的多选。
$('#your-select').multiSelect('refresh');
.multiSelect(’addOption’,Hash)
动态添加选项到多选。
选项Hash如下所述:
key | type | required | desription |
---|---|---|---|
value | String | true | 要创建的选项的值 |
text | String | true | 要创建的选项的文本 |
index | Number | false | 索引在何处插入选项。 如果没有给出,它将作为最后一个选项插入。 |
nested | String | false | 如果有optgroups,您可以选择要在哪个optgroup下插入选项。 |
$('#your-select').multiSelect('addOption', { value: 'test', text: 'test', index: 0, nested: 'optgroup_label' });
Keyboard
key | function |
---|---|
[ ↓ ] Down arrow | Select next item in the focused list |
[ ↑ ] Up arrow | Select previous item in the focused list |
[ — ] Space | Add/remove item depending on which list is currently focused |
[ ← ] Left arrow | Focus the previous list |
[ → ] Right arrow | Focus the next list |
Demos
Pre-selected options
<select id='pre-selected-options' multiple='multiple'>
<option value='elem_1' selected>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4' selected>elem 4</option>
<option value='elem_5'>elem 5</option>
</select>
$('#pre-selected-options').multiSelect();
Callbacks
<select id='callbacks' multiple='multiple'>
<option value='elem_1'>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4'>elem 4</option>
<option value='elem_5'>elem 5</option>
</select>
$('#callbacks').multiSelect({
afterSelect: function(values){
alert("Select value: "+values);
},
afterDeselect: function(values){
alert("Deselect value: "+values);
}
});
Keep Order:
注意:如果原始选择中有optgroup,则此功能将不起作用。
<select id='keep-order' multiple='multiple'>
<option value='elem_1'>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4'>elem 4</option>
<option value='elem_5'>elem 5</option>
</select>
$('#keep-order').multiSelect({ keepOrder: true });
Public methods
<a href='#' id='select-all'>select all</a>
<a href='#' id='deselect-all'>deselect all</a>
<a href='#' id='select-10'>select 10 elems</a>
<a href='#' id='deselect-10'>deselect 10 elems</a>
<select id='public-methods' multiple='multiple'>
<option value='elem_1'>elem 1</option>
<option value='elem_2' disabled>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4'>elem 4</option>
<option value='elem_5'>elem 5</option>
<option value='elem_6'>elem 6</option>
<option value='elem_7'>elem 7</option>
<option value='elem_8'>elem 8</option>
<option value='elem_9'>elem 9</option>
<option value='elem_10'>elem 10</option>
</select>
$('#public-methods').multiSelect();
$('#select-all').click(function(){
$('#public-methods').multiSelect('select_all');
return false;
});
$('#deselect-all').click(function(){
$('#public-methods').multiSelect('deselect_all');
return false;
});
$('#select-10').click(function(){
$('#public-methods').multiSelect('select', ['elem_0', 'elem_1' ..., 'elem_9']);
return false;
});
$('#deselect-10').click(function(){
$('#public-methods').multiSelect('deselect', ['elem_0', 'elem_1' ..., 'elem_9']);
return false;
});
$('#refresh').on('click', function(){
$('#public-methods').multiSelect('refresh');
return false;
});
$('#add-option').on('click', function(){
$('#public-methods').multiSelect('addOption', { value: 42, text: 'test 42', index: 0 });
return false;
});
Optgroup
<select id='optgroup' multiple='multiple'>
<optgroup label='Friends'>
<option value='1'>Yoda</option>
<option value='2' selected>Obiwan</option>
</optgroup>
<optgroup label='Enemies'>
<option value='3'>Palpatine</option>
<option value='4' disabled>Darth Vader</option>
</optgroup>
</select>
$('#optgroup').multiSelect({ selectableOptgroup: true });
Disabled attribute
<select id='disabled-attribute' disabled='disabled' multiple='multiple'>
<option value='elem_1'>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4'>elem 4</option>
<option value='elem_5'>elem 100</option>
</select>
$('#disabled-attribute').multiSelect();
Custom headers and footers
<select id='custom-headers' multiple='multiple'>
<option value='elem_1'>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4'>elem 4</option>
<option value='elem_5'>elem 5</option>
</select>
$('#custom-headers').multiSelect({
selectableHeader: "<div class='custom-header'>Selectable items</div>",
selectionHeader: "<div class='custom-header'>Selection items</div>",
selectableFooter: "<div class='custom-header'>Selectable footer</div>",
selectionFooter: "<div class='custom-header'>Selection footer</div>"
});
Searchable
注意:此功能不是内置的,但取决于其他插件。 我个人使用优秀的quicksearch库,但你可以使用你喜欢的任何库。
$('.searchable').multiSelect({
selectableHeader: "<input type='text' class='search-input' autocomplete='off' placeholder='try \"12\"'>",
selectionHeader: "<input type='text' class='search-input' autocomplete='off' placeholder='try \"4\"'>",
afterInit: function(ms){
var that = this,
$selectableSearch = that.$selectableUl.prev(),
$selectionSearch = that.$selectionUl.prev(),
selectableSearchString = '#'+that.$container.attr('id')+' .ms-elem-selectable:not(.ms-selected)',
selectionSearchString = '#'+that.$container.attr('id')+' .ms-elem-selection.ms-selected';
that.qs1 = $selectableSearch.quicksearch(selectableSearchString)
.on('keydown', function(e){
if (e.which === 40){
that.$selectableUl.focus();
return false;
}
});
that.qs2 = $selectionSearch.quicksearch(selectionSearchString)
.on('keydown', function(e){
if (e.which == 40){
that.$selectionUl.focus();
return false;
}
});
},
afterSelect: function(){
this.qs1.cache();
this.qs2.cache();
},
afterDeselect: function(){
this.qs1.cache();
this.qs2.cache();
}
});
<select id='custom-headers' multiple='multiple'>
<option value='elem_1' selected>elem 1</option>
<option value='elem_2'>elem 2</option>
<option value='elem_3'>elem 3</option>
<option value='elem_4' selected>elem 4</option>
<option value='elem_5'>elem 100</option>
</select>