我在 select2 下拉串列中添加了一個“添加新人”按鈕
select2 代碼如下所示:
$('#subFundSelection').select2({
width: '100%',
placeholder: 'Select a sub-fund',
ajax: {
url: '/FundInfo/SearchLegalEntities',
type: 'GET',
dataType: 'json',
delay: 250,
allowClear: true,
data: function (params) {
return {
searchTerm: params.term
};
},
processResults: function (data) {
return {
results: $.map(data.legalEntities, function (obj) {
return { id: obj.Id, text: obj.Text };
})
};
},
},
minimumInputLength: 2
}).on('select2:open', () => {
$(".select2-results:not(:has(button))").append('<button id="newPersonButton" style="width: 100%" type="button" onClick="CreateLegalEntity()"> Add a new person</button>')
})
一個按鈕很好地添加到下拉串列中(對不起,我不能添加圖片)
當我點擊這個“添加新人”按鈕時,我打開了一個模式。
function CreateLegalEntity() {
$('#CreateLegalEntityModal').modal('show');
}
一切正常,而不是點擊后下拉串列沒有關閉,它出現在模式的頂部,直到我點擊螢屏上的其他地方。
抱歉沒有附上截圖,但網站不允許我。
我試過了:
$(this).blur()
$(".select2").trigger('blur')
$(".select2").trigger('change')
uj5u.com熱心網友回復:
從此處的 SELECT2 檔案方法 | 選擇2
close 方法將導致下拉選單關閉,隱藏可選選項:
$('.select2').select2('close');
應該做你想要的
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/366603.html
標籤:javascript 落下 jquery-select2
