【问题标题】:How to repopulate select2 result如何重新填充 select2 结果
【发布时间】:2016-04-14 03:40:30
【问题描述】:

早安,

我正在为我的项目使用 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
                }
          });
      });

【问题讨论】:

    标签: jquery jquery-select2 select2


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-30
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 2014-04-28
      • 2018-08-01
      相关资源
      最近更新 更多