当我尝试将选项动态添加到 Bootstrap 可搜索选择时,它是在选择之外添加的。请看截图
如何正确地将选项动态添加到 Bootstrap 可搜索选择
这是我的代码
<div class="row form-group">
<select class="col form-control selectpicker supplierSelect" data-live-search="true"
title="Select Supplier..." data-size="5">
<option th:each="supplier : ${suppliers}" th:value="${supplier.id}" th:text="${supplier.name}"></option>
</select>
<button type="button" class="btn mb-1 btn-rounded btn-outline-info"
data-toggle="modal" data-target="#addSupplier"><span><i class="fa fa-plus"></i> </span>
</button>
</div>
这就是我添加数据的方式
document.querySelector(domStrings.saveSupplierButton).addEventListener('click', function () {
const data = JSON.stringify(uiController.getSupplierData());
console.log(data);
const url = window.location.protocol + "//" + window.location.hostname+":"+window.location.port+"/supplier";
const otherParams = {
headers: {"content-type": "application/json; charset=UTF8"},
body: data,
method: "POST"
};
fetch(url, otherParams)
.then(data=>{return data.json()})
.then(data => {
console.log(data);
let template = '<option value="{optVal}">{text}</option>';
template = template.replace('{optVal}',data['id']).replace('{text}',data['name']);
$(domStrings.supplierSelect).append($.parseHTML(template));
$(domStrings.supplierSelect).selectPicker('refresh');
//fixme
})
.catch(error=>console.log(error))
});
但这不起作用。我的代码有什么问题?