//顶部引入css
<th:block th:include="include :: select2-css" />
<th:block th:include="include :: bootstrap-select-css" />
//html页尾引入js
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: bootstrap-select-js" />
select标签
<div class="form-group">
<label class="col-sm-3 control-label">可以做的产品:</label>
<div class="col-sm-8">
<select id="processIds" name="processIds" class="form-control select2-multiple" multiple>
</select>
</div>
</div>
jq
$(document).ready(function() {
var str = [[${employee.processIds}]];//id数组字段
var arr = str.split(",");
var intArr = arr.map(function (item) {
return parseInt(item, 10);
});
$.ajax({
url: '/process/process/list',
method: 'POST',
success: function(res){
var html = "";
for(var i = 0; i < res.rows.length; i++){
var row = res.rows[i];
if(intArr.includes(row.id)) {
html += '<option value=' + row.id + ' selected>' + row.name + '</option>';
}else{
html += '<option value=' + row.id + '>' + row.name + '</option>';
}
}
$("#processIds").html(html);
},
error: function(){
console.log()
}
})
});