效果如下,选中值显示在文本框中,可以点击叉号去掉选择。
做法如下:
引入css样式:
<link href="~/select2/css/select2.css" rel="stylesheet" />
引入js:
<script src="~/select2/js/select2.js"></script>
js和css可以在网络上下载,我这里假设已经下载到本地,
html部分
<button class="close" type="button" id="sel_all"></button>
<select class="form-control" id="sel_choice" multiple="multiple" style="width: 350px; border: #ced4da"></select>
js部分:
$('#sel_choice').select2({allowClear: true,placeholder: {id: '-1',text: ''}
});//清除所有选择
$('#sel_all').click(function (event) {event.preventDefault();var res = [];$(this).next('select').find('option').each(function (i, ele) {res.push($(ele).val())});$('#sel_choice').val(res).trigger('change');
});