【问题标题】:populate select picker on live-search event在实时搜索事件中填充选择选择器
【发布时间】:2017-05-24 23:01:05
【问题描述】:
【问题讨论】:
标签:
jquery
bootstrap-selectpicker
【解决方案1】:
您将需要 javascript。
这里是一个使用 jquery 的例子:
首先在 HTML 中,您需要将您的选择元素包装到一个带有 id 的 div 中,这样您就可以使用表单控件类来使用它的输入
<div id="my-div">
<select id="my-select" class="selectpicker" data-live-search="true"></select>
</div>
现在在 javascript 文件中,您需要执行以下操作
$('#my-select').selectpicker('refresh');
$('#my-div .form-control').on('keyup', function () {
//here you listen to the change of the input corresponding to your select
//and now you can populate your select element
$('#my-select').append($('<option>', {
value: 'any value',
text: 'any text'
}));
$('#my-select').selectpicker('refresh');
});
【解决方案2】:
由于boostrap-select (or selectpicker) 还不支持此功能,我找到了解决方法。
这不是最佳做法,但确实有效。
$('#youselectpicker').on('loaded.bs.select', function (e, clickedIndex, isSelected, previousValue) {
$(".youselectpicker .bs-searchbox input").on('keyup',function(){
//place your code here
//don't forget to refresh the select picker if you change the options
})
});
【解决方案3】:
我的简单 jQuery 实现有效。
jQuery("select[name='select picker name']").siblings().find("input[type='text']").keyup(function (e) {
if(e.keyCode==13){
//Code to implement ajax search
}
});