开发者

Can't get jQuery autocomplete to return anything. What am I doing wrong?

开发者 https://www.devze.com 2023-01-08 18:50 出处:网络
I\'m using some Javascript I found from a post on StackOverflow. When I start entering text into the input, a spiny loading graphic appears within the input element, however nothing actually appears.

I'm using some Javascript I found from a post on StackOverflow. When I start entering text into the input, a spiny loading graphic appears within the input element, however nothing actually appears.

When I go to my URL that r开发者_开发百科eturns the JSON encoded string just to test it, it seems to work fine:

http://xxxxxxxx/app/get_clients/test (get_clients is the function, test is the string to search for)

Returns:

["Testing","Testing1","test11","test4","Testing21","Just Testing","testy"]

Any idea what I'm doing wrong?

Here is my JavaScript:

format_item = function (item, position, length)
{ 
 return item.title; 
} 

prep_data = function(data)
{ 
 tmp = $.evalJSON(data); 
 parsed_data = []; 
 for (i=0; i < tmp.length; i++)
 { 
 obj = tmp[i]; 
  parsed_data[i] =
  { 
  data: obj , 
  value: obj.isbn13, 
  result: obj.title 
  }; 
 }

 return parsed_data 
}

$(document).ready(function()
{
 $("#file_hide").hide();

 $("#<?php echo $this->form_name['company']; ?>").autocomplete({ 
  url : "<?php echo site_url("app/get_clients"); ?>", 
  parse: prep_data, 
  formatItem: format_item, 
 }); 
});

And my PHP:

 function get_clients($s)
 {
  $this->db_common->like('name', $s);
  $query = $this->db_common->get('clients');

  $results = array();

  foreach ($query->result_array() as $row):
     $results[] = $row['name'];
  endforeach;

  echo json_encode($results);
 }


Demo page has link to sample php backend implementation.

http://jquery.bassistance.de/autocomplete/demo/

In short, you need to use request parameter (q by default).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号