1,添加select2 樣式 參考(https://select2.org/? )??
2,Html:
?<select id="txtType" name="Type" class="form-control select2" multiple="multiple">
</select>
3,jquery
@section scripts{
? $(document).ready(function () {
var selectUpload = {
method: function () {
$.ajax({
type: "get",
url: "/Backstage/SelecType",
success: function (SelecList) {
for (var i = 0; i < SelecList.length; i++) {
var id = SelecList[i].Id;
var type = SelecList[i].英雄類型;
console.log('英雄類型 load success');
$("#txtType").append("<option value='" + id + "'>" + type + "</option>");
$('.select2').select2({
allowClear: true,
language: "zh-CN"? ? ?
});? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
}
}
})
}
}
selectUpload.method();
}
? });
封裝function??
效果圖:
點擊添加
4, jquery 動態加載:
$.ajax({
type: "post",
url: "/Backstage/GetTypeIds",
data: { Id: rowdata.Id },
success: function (typeIds) {
console.log(typeIds);
$('#txtType').val(typeIds).trigger('change');? ?(typeIds 后臺返回的是 List<int>類型 )
}
});
效果圖:
后臺加載出來