【问题标题】:Select2 is showing "No result found" message when selecting second timeSelect2 在第二次选择时显示“未找到结果”消息
【发布时间】:2020-04-29 09:23:47
【问题描述】:

我正在使用带有 ajax 选项的 Select2 及其多选框。我可以第一次选择记录。但是在选择第一个之后,如果我尝试选择下一个,它就不起作用了。

fiddle link for complete code

$('#select2_ajax_complex_id').select2({
    tags: true,
    maximumSelectionSize: 10,
    minimumResultsForSearch: Infinity,
    multiple: true,
    minimumInputLength: 1,
    placeholder: "Search Employee",
    //data:o,
    id: function(i) {
      return i;
    },
    initSelection: function(element, callback) {

    },
    ajax: {
      type: 'post',
      url: "/echo/json/",
      allowClear: true,
      dataType: 'json',
      delay: 250,
      params: {
        contentType: "application/json"
      },
      data: function(term, page) {
        //Code for dummy ajax response
        return {
          json: complex_employee_response,
          delay: 0
        };
      },
      results: function(data, page) {
        return {
          results: data
        };
      },
      cache: false
    },
    formatResult: function(i) {
      return '<div>' + i.name + '(' + i.role + ')' + '</div>';
    }, // Formats results in drop down
    formatSelection: function(i) {
      return '<div>' + i.name + '(' + i.role + ')' + '</div>';
    }, //Formats result that is selected
    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
    escapeMarkup: function(m) {
        return m;
      } // we do not want to escape markup since we are displaying html in results          
  })

有人可以看看并帮助解决这个问题吗?

【问题讨论】:

  • 我已经编辑了您提供的演示。问题已在其中解决。请参阅相同的答案

标签: javascript jquery jquery-select2


【解决方案1】:

从您的代码中删除下面的块

//data:o,
id: function(i) {
  return i;
},
initSelection: function(element, callback) {

},

通过使用此代码,您的问题将得到解决

我为此创建了一个演示[在您的示例中编辑]

只要参考这个网址

https://jsfiddle.net/7m2nv5yw/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 2013-09-24
    • 1970-01-01
    • 2013-12-25
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多