首先引入select2相关文件
然后初始化
var config={
with:'95%',
placeholder: '最多可添加20个标签!',
formatNoMatches:'Oops, nothing found!',
maximumSelectionLength:20,//最多选择20
multiple: true,
templateResult: function (data) {
var $result = $('<span>'+data.text+'</span><i data-id="' + data.id +' " class="btntag btn-select-pessoa-fisica-frm-pessoa-fisica-parentesco" style="float:right;height:20px;width:20px;" onclick="return false">x</i>');
return $result;
},
}
$('.tag_list').select2(config);
templateResult这个配置项是可以生成图片的,但是会有问题点击会触发下拉多选
$(document).on('mouseup', '', function (e) {
$.ajax({
type: 'get',
url: '',
data: {},
success: function (msg) {
if(msg==1){
$('.tag_list').find("option[value="+deleoptid+"]").remove();
$('.tag_list').select2(config);
}
},
async: false,
dataType: "json",
});
$('.tag_list').select2(config);
return false
});
$(document).on('mouseup', '', function (e) {});这个方法不会触发select2的多选就可以在里面写事件了