如何重新填充select2结果

时间:2016-04-14 03:40:30

标签: jquery jquery-select2 select2

美好的一天,

我正在为我的项目使用Select2插件。我在收件人文本的电子邮件功能中使用它。首先,当用户输入将从数据库中提取的内容时,它将在我的select中添加新选项。我的问题是如何重新填充select2的结果,而不是显示没有结果。

以下是我的HTML代码

<div class="form-group" id="div_select2">
     <select class="form-control select2" id="select2_email" multiple="multiple" data-placeholder="Select a Group" style="width: 100%;">
          <option></option>
          <option value="group" data-id="1">Pulse Asia Group</option>
          <option value="group" data-id="2">Unite Group</option>
          <option value="group" data-id="3">RCBC Group</option>
     </select>
</div><!-- /.form-group -->

我的脚本示例如下

$(document).ready(function(){
        $('#div_select2').on('keyup','.select2-search__field',function(){
            keyme = $(this);
            $(this).attr('added','current');
            console.log($('#select2-select2_email-results li').eq(0));
            if( typeof $('#select2-select2_email-results li').eq(0).attr('id') == 'undefined'){ //select2 no result
                l = '<option class="added-opt" value="group" data-id="">'+$(this).val()+'</option>';
                $('#select2_email').append(l);
                //i want to trigger the repopulate of results here so the user can choose dynamic results
                }
          });
      });

1 个答案:

答案 0 :(得分:0)

我使用了select2 ajax选项。它可以使用ajax方法获取数据。