Bootstrap typeahead不能使用我的以下代码

时间:2013-07-24 02:41:54

标签: jquery twitter-bootstrap bootstrap-typeahead

我有一个搜索地方的代码。服务器正在以正确的json格式返回数据,但是typeahead没有显示结果。

<script type="text/javascript">
$(document).ready(function(e) {
    $('#txt_ser').typeahead({
      minLength:1,
      source: function (query, process) {
          var places = [];
          var map = {};
          $.ajax({
              dataType: "json",
              url: "<?php echo base_url() . "ajax/ser";?>",
              data: 'q='+query,
              type: 'POST',
              success: function (data) {
                  $.each(data, function(i, place){
                      map[place.yt_center_state] = place;
                      places.push(place.yt_center_state);
                  });
                  return process(places);
               }
          })
      }
    });
});
</script>

服务器以json格式返回数据,当键入关键字pune时,下面显示了一个示例

0: {yt_center_top_city:pune, yt_center_state:MH}
1: {yt_center_top_city:pune, yt_center_state:MH}
2: {yt_center_top_city:pune, yt_center_state:MH}
3: {yt_center_top_city:pune, yt_center_state:MH} 

1 个答案:

答案 0 :(得分:0)

您没有将ajax请求的结果返回到预先source。这段代码:

 return process(places);

位于success的{​​{1}}处理程序内,因此它只会将$.ajax返回给该函数。

您应该将代码更改为:

places

希望有所帮助