【问题标题】:What is the right way to clear select2 dropdown when using Ajax?使用 Ajax 时清除 select2 下拉列表的正确方法是什么?
【发布时间】:2016-03-11 04:02:27
【问题描述】:

我遇到了select2 的问题,在我输入一个项目并且我想再次使用相同的下拉菜单后,我似乎无法删除之前选择的项目

例如,假设我的 id 是“#StreamName”,我认为这会清除它:

 $("#StreamName").val(null).trigger("change");

但它似乎仍然显示上一个选定的项目。从查看 html 看来,我这样做是蛮力的:

$("#select2-StreamName-container").html("");

但这似乎我没有正确使用 API,所以我想看看是否有其他建议或者我是否遗漏了什么。

【问题讨论】:

    标签: jquery jquery-select2 jquery-select2-4


    【解决方案1】:

    Working Fiddle - Local Data Source

    看起来清除 Select2 的正确方法是您最初建议的 this answer 建议的方法。

    Javascript

    $(document).ready(function() {
      $("#StreamName").select2();
      $("#StreamName").val(null).trigger("change");
    });
    

    HTML

    <select id="StreamName">
      <option value="AL">Alabama</option>
      <option value="CA">California</option>
      <option value="WY">Wyoming</option>
    </select>
    

    更新 - 远程数据源

    JSFiddle Using Remote Data

    即使在使用远程数据源时,这仍然可以清除 Select2 输入:

    $("#StreamName").val(null).trigger("change");
    

    完整的 Javascript

    $(document).ready(function() {
      $("#StreamName").select2({
        ajax: {
          url: "https://api.github.com/search/repositories",
          dataType: 'json',
          delay: 250,
          data: function(params) {
            return {
              q: params.term, // search term
              page: params.page
            };
          },
          processResults: function(data, params) {
            // parse the results into the format expected by Select2
            // since we are using custom formatting functions we do not need to
            // alter the remote JSON data, except to indicate that infinite
            // scrolling can be used
            params.page = params.page || 1;
    
            return {
              results: data.items,
              pagination: {
                more: (params.page * 30) < data.total_count
              }
            };
          },
          cache: true
        },
        escapeMarkup: function(markup) {
          return markup;
        }, // let our custom formatter work
        minimumInputLength: 1,
        templateResult: formatRepo, // omitted for brevity, see the source of this page
        templateSelection: formatRepoSelection // omitted for brevity, see the source of this page
      });
    
    
      $("#StreamName").val(null).trigger("change");
    
      // For testing...
      $('#set-null').click(function() {
        $("#StreamName").val(null).trigger("change");
      });
    
    
    
    });
    
    
    function formatRepo(repo) {
      if (repo.loading) return repo.text;
    
      var markup = "<div class='select2-result-repository clearfix'>" +
        "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
        "<div class='select2-result-repository__meta'>" +
        "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
    
      if (repo.description) {
        markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
      }
    
      markup += "<div class='select2-result-repository__statistics'>" +
        "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
        "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
        "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
        "</div>" +
        "</div></div>";
    
      return markup;
    }
    
    function formatRepoSelection(repo) {
      return repo.full_name || repo.text;
    }
    

    HTML

    <select id="StreamName">
      <option value="3620194" selected="selected">select2/select2</option>
    </select>
    

    【讨论】:

    • 就我而言,我没有预先定义选项列表。我根据此页面上的示例从远程数据源填充:select2.github.io/examples.html 出于某种原因,在此用例中,执行 $("#StreamName").val(null).trigger( “改变”);
    • 我更新了答案以包含远程数据源。也许你的源代码中有其他东西导致它失败?如果我能提供帮助,请告诉我。
    • 有趣的是,这种重置方法对我也不起作用。必须 sed html('') 才能使其工作
    【解决方案2】:

    我也有这个问题

    $("#StreamName").val(null).trigger("change");
    

    将 select2 选项设置为空白 HTML 对我有用。

    $("#StreamName").html("").trigger("change");
    

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2015-10-17
      • 2012-12-06
      • 2018-03-26
      • 1970-01-01
      相关资源
      最近更新 更多