预先输出发送请求但不显示已提取的结果

时间:2015-08-11 20:51:56

标签: jquery typeahead

这是我使用过的代码并尝试了4个小时,可以看到ajax请求带来jason reults但是输入字段中没有建议

$('#user-search').typeahead({
name: 'user-search',
remote: '/search.php?query=%QUERY' 
});

var student = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: 'php/search_groups.php',
    wildcard: '%QUERY'
  }
});

student.initialize();

$('#students').typeahead(null, {
  name: 'students',
  displayKey: 'value',
  source: student,
});

<div class="form-group">
      <label for="tags">
 Assign To</label>
       <input  id="students" type="text" class="typeahead form-control" value="" name="students" placeholder="Specify for special students" />
</div>[![enter image description here][1]][1]

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

 var students = new Bloodhound({
  datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name);},
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  valueKey: 'name',
  remote: {
    url: 'php/search_groups.php?search=%QUERY',
    wildcard: '%QUERY',
    filter: function (students) {
      // Map the remote source JSON array to a JavaScript object array
      return $.map(students.data, function (student) {
          return student;
      });
    }
  }
});
student.initialize();

要将Typeahead指定给您可以执行的输入:

$('.add-employment-city').typeahead({
      hint: true,
      highlight: true,
      minLength: 1
    },{
      name: 'students',
      displayKey: 'name',
      source:students.ttAdapter(),
    });

在PHP文件中你应该把它放在:

$search = $_GET['search'];
$students = //USE YOUR SQL CODE TO SEARCH

return json_encode(['data'=>$students]);

提示:将结果名称设置为复数(学生),因为您经常会收到很多结果。