【问题标题】:Unable to select result from select2 dropdown无法从 select2 下拉列表中选择结果
【发布时间】:2016-07-09 10:46:23
【问题描述】:

我有以下通过 AJAX 提取数据的 javascript/jQuery。效果很好,但是我似乎无法从下拉列表中选择一个选项?谁能解释我做错了什么..

这和id有关..

$(".js-data-example-ajax").select2({
  ajax: {
   url: "/admin/generator/teams",
   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,
 id: function(data){ return data._id; },
});

【问题讨论】:

    标签: javascript jquery json select select2


    【解决方案1】:

    首先,您应该确保processResults 函数返回一个具有idtext 属性的对象数组,如下所示:

    { id: '1', text: 'Option 1' }
    

    然后一旦选项被加载,你可以通过调用来设置值:

    $(".js-data-example-ajax").val('1').trigger('change');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 2019-10-27
      • 2016-04-14
      相关资源
      最近更新 更多