对于选择菜单插件
chosen.js,是否有既定的方法将“选择列表中的所有项目”或“将列表中的所有项目”功能添加到多选输入?在主分支或者可能是其中一个分叉?或者有人这样做之前有一些提示吗?
解决方法
它应该是非常直接的,只需先按照“正常”的方式进行:
$('.my-select-all').click(function(){
$('#my_select option').prop('selected',true); // Selects all options
});
然后触发liszt:updated事件来更新所选的小部件,所以整个事情看起来像这样:
更新:对于那些没有scroll down and check the other answers的人,该活动被选中:自2013年8月发布的版本更新。如有疑问,请咨询documentation。
<select multiple> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <button class="select">Select all</button> <button class="deselect">deselect all</button>
$('select').chosen();
$('.select').click(function(){
$('option').prop('selected',true);
$('select').trigger('liszt:updated');
});
$('.deselect').click(function(){
$('option').prop('selected',false);
$('select').trigger('liszt:updated');
});
工作演示(js代码位于底部):http://jsfiddle.net/C7LnL/1/
更严格的版本:http://jsfiddle.net/C7LnL/2/
更紧凑的版本:http://jsfiddle.net/C7LnL/3/