为什么bootstrap selectjs
不适用于动态附加值。
每当我从javascript
添加新选项时,它都不会反映到实时搜索中。
是代码段
setTimeout(function(){
$('#demo').append('<option>India</option>');
console.log('appended');
},1000);
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.js"></script>
<select id="demo" class="selectpicker" data-live-search="true" >
<option>Alabama</option>
<option>Alaska </option>
<option>Arizona </option>
</select>
答案 0 :(得分:0)
使用refresh
方法使用新附加选项刷新选择器。
$('#demo').selectpicker('refresh');
参考:
https://silviomoreto.github.io/bootstrap-select/methods/#selectpickerrefresh
答案 1 :(得分:0)
您的方法是在添加新元素后附加列表需要refreshed的新选项。另请注意,display:none
设置为默认HTML选择标记
setTimeout(function() {
$('#demo').append('<option>India</option>').selectpicker('refresh');
console.log('appended');
}, 1000);
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/css/bootstrap-select.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.3/js/bootstrap-select.js"></script>
<select id="demo" class="selectpicker" data-live-search="true">
<option>Alabama</option>
<option>Alaska </option>
<option>Arizona </option>
</select>