Select2 Ajax方法不选择

16 浏览
0 Comments

Select2 Ajax方法不选择

好的,我确定这里有一些简单的设置错误,但我不确定是什么。

所以我正在尝试使用Select2 AJAX方法作为用户搜索数据库并选择结果的方式。调用本身返回了结果,但是不允许我从列表中选择答案。它也似乎不允许你在悬停或上下箭头时“选择”它。所以,不多说了,这是我的代码:

index.html


    
        
        
        
        
    
    
        
    

select.js

jQuery(function() {
  var formatSelection = function(bond) {
    console.log(bond)
    return bond.name
  }
  var formatResult = function(bond) {
    return '' + bond.name + ''
  }
  var initSelection = function(elem, cb) {
    console.log(elem)
    return elem
  }
    $('.select2').select2({
      placeholder: "Policy Name",
      minimumInputLength: 3,
      multiple: false,
      quietMillis: 100,
      ajax: {
        url: "http://localhost:3000/search",
        dataType: 'json',
        type: 'POST',
        data: function(term, page) {
          return {
            search: term,
            page: page || 1
          }
        },
        results: function(bond, page) {
          return {results: bond.results, more: (bond.results && bond.results.length == 10 ? true: false)}
        }
      },
      formatResult: formatResult,
      formatSelection: formatSelection,
      initSelection: initSelection
    })
})

JSON响应

{
  error: null,
  results: [
    {
      name: 'Some Name',
      _id: 'Some Id'
    },
    {
      name: 'Some Name',
      _id: 'Some Id'
    }
  ]
}

一切似乎都正确引入,但我无法实际选择答案并将其输入到框中。我的代码中是否有问题?

0