jQuery UI自动完成有问题 [英] Having problems with jQuery UI Autocomplete

查看:85
本文介绍了jQuery UI自动完成有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jQuery UI中使用新的自动完成功能,但是我遇到了一些问题.

I'm trying to use the new autocomplete function in jQuery UI, but I'm having some problems.

我能够从数据库中检索数据(我可以在FireBug中看到它),但是我无法显示下拉列表(或提醒数据).

I'm able to retrieve data from the DB (I can see it in FireBug), but I'm not able to display the dropdown list (or alert the data).

这是我的jQuery代码:

This is my jQuery code:

  jQuery('#brand_search').autocomplete({
            source: "http://mysite.com/wp-content/themes/storelocator/include/jquery.search.php?instance=brand",
            minLength: 2,
            delay: 50,
            select: function(e, ui) {
              alert(ui);
            }
        });

这是我的PHP代码:

/* ------------------ Brand Autosuggest ------------------------- */
  function autosuggestBrand($dal)
  { 
    $result = $dal->getRowBySearch('sl_label','name', $this->term);   
    $brands = array();

    if(mysql_num_rows($result)>0) 
    {
      while($row = mysql_fetch_assoc($result)) 
      {
          array_push($brands, array(
            "id"      => $row['id'], 
            "name"    => html_entity_decode($row['name'], ENT_QUOTES, 'UTF-8') )
        );
      }     
    }
    echo json_encode($brands);          
  }

我看过以下两个指南:
http://www.jensbits .com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql
http://net.tutsplus .com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget

I've see these two guides:
http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget

但是仍然不太清楚如何显示/警告获取的数据.

But still can't quite figure out how to display / alert the fetched data.

这是echo json_encode

[
  {"id":"4642","name":"Mo Koshji"},
  {"id":"4627","name":"MO-A"},
  {"id":"4626","name":"MO'CYCLE"},
  {"id":"4628","name":"mo851"},
  {"id":"4629","name":"Mob Action"}
]

推荐答案

使用以下命令更正您的php数组,以便为​​jquery-autocomplete获得正确的json输出:

correct your php array with the following in order to get a correct json output for jquery-autocomplete:

array_push
(
   $brands,
   array
   (
      "label"  => $row['id'], 
      "value"  => html_entity_decode($row['name'], ENT_QUOTES, 'UTF-8') )
    );
);

因为jquery自动完成功能需要这些json属性名称来运行文档中指定的自动完成功能:

because jquery autocomplete needs those json property names to run the autocomplete as specified on the documentation:

本地数据可以是一个简单的数组 的字符串,或包含用于的对象 数组中的每个项目,其中一个 标签或值属性,或两者兼而有之.这 标签属性显示在 建议菜单.

The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu.

http://jqueryui.com/demos/autocomplete/#custom-data

这篇关于jQuery UI自动完成有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆