固然,首先須要引入,select2.css,jquery,boostrap.js,select2.jscss
多選 添加屬性jquery
function formatRepoProvince(repo) { if (repo.loading) return repo.text; var markup = repo.name; return markup; } function formatRepoSelection (repo) { return repo.name || repo.text; } // 遠程篩選 $(".select2").select2({ ajax: { url: 'v2/enterprise/searchListPaging.do', delay: 500, method: "post", data: function (params) { return { keyword: params.term ? params.term : '', skip: 0 } }, processResults: function (data) { return { results: data.data.items, } }, }, placeholder: '公司名稱', escapeMarkup: function (markup) { return markup; }, // let our custom formatter work minimumInputLength: 1, templateResult: formatRepoProvince, templateSelection: formatRepoSelection })
踩坑點git
下拉框若是在bootstrap模態框中,input不會聚焦github
解決方案1:
在bootstrap.js中修改:
Modal.prototype.enforceFocus = function () {
$(document)
.off('focusin.bs.modal') // guard against infinite focus loop
.on('focusin.bs.modal', $.proxy(function (e) {ajax
//如下爲加入代碼
if ($(e.target).hasClass('select2-search__field')) {
return true;
}
//加入代碼結束bootstrap
if (this.$element[0] !== e.target && !this.$element.has(e.target).length) {
this.$element.trigger('focus')
}
}, this))
}oop
解決方案2:post
說明綁定給哪一個父級this