如果选项尚不存在,如何从搜索中将选项添加到引导选择?

Zar*_*ana 2 jquery jquery-ui jquery-plugins bootstrap-selectpicker

我正在使用bootstrap-select

select如果我在使用 的搜索菜单中找不到新选项data-live-search="true",因为它还不存在,我该如何添加新选项?

Ran*_*osh 5

请按照以下代码操作::

超文本标记语言

<select class="selectpicker" id="selectpicker" data-live-search="true">
  <option data-tokens="ketchup mustard">Hot Dog, Fries and a Soda</option>
  <option data-tokens="mustard">Burger, Shake and a Smile</option>
  <option data-tokens="frosting">Sugar, Spice and all things nice</option>
</select>
Run Code Online (Sandbox Code Playgroud)

CSS

.no-results{
    cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)

查询

$(document).ready(function (e) {
    $(document).on('click', 'li.no-results', function () {
        var new_option = $(this).text().split('"')[1];
        $("#selectpicker")
       .append('<option>'+ new_option +'</option>')
       .selectpicker('refresh');
       //You can also call AJAX here to add the value in DB
    });
});
Run Code Online (Sandbox Code Playgroud)

请根据需要更改这些代码。
工作 Jsfiddle 链接:https://jsfiddle.net/ktmv1vxh/1/