无法用数据实时搜索填充引导程序选择+使用jquery填充多个

Can not populate bootstrap select with data live search + multiple with jquery

本文关键字:填充 使用 jquery 选择 引导程序 数据 实时 搜索      更新时间:2023-09-26

我想用从 php 代码获取的选项填充以下 select 语句

<select name='friends[]' id='friends' class='selectpicker show-tick form-control'
        data-live- search='true' multiple>
    <!-- options here -->
</select>

我的j查询代码

$.ajax({
    url:'get_togethers.php', //this returns object data
    data:'user_id='+user_id,
    type:'POST',
    datatype:'json',
    success:function(data) { //data = {"0":{"id":1,"name":"Jason"},"1":{"id":2,"name":"Will"},"length":2 }
        data = JSON.parse(data);
        var options;
        for (var i = 0; i < data['length']; i++) {
            options += "<option value='"+data[i]['id']+"'>"+data[i]['name']+"</option>";
        }
        $("#friends").append(options);
    }
});

显示 select 标记内的静态值,但不会显示从 ajax 函数添加的值。编辑:如果我从中删除引导程序,则会显示值,但是打开引导程序时,它们不会显示。

$('#friends').selectpicker('refresh');

有必要更新我错过的新添加的值。