jquery.multiselect 应该用得还是挺多,它有比较美观的界面,以及对html select控件进行多选扩充的实用功能。对于下拉列表选项较多时,如果将选中的项默认排到最前面,这将会大大提高用户使用便利。例如:
这样再次点开,还会是这种效果,像上面选项很多的情况,用户就不方便直观的看到自己选择了哪些选项。好在控件提供了event,我们可以在它展开之前进行“排序”,我们需要使用官方提供的beforeopen 事件,监听这个事件。
$("#yourId").multiselect({ beforeopen: function(event, ui){ var selected = $("select[id=yourId] option:selected"); selected.remove(); $("select[id=yourId]").prepend(selected); $("#yourId").multiselect('refresh'); } });
假设yourId是你html select 的id,在document ready()事件中给这个select添加个事件监听后,我们再看效果:
multiselect 还有很多有意思的方法和事件,属性,具体可以看官方文档:
http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/