dtcrw26206 2013-01-06 17:03
浏览 21
已采纳

Jquery自动完成“几乎”工作,但列表不完全填充

I'm trying to use autocomplete in jquery, and it works with the demo data, but I haven't been able to make it work with my own data source. I'm trying to write a mailer where the user just enters a few letters of a person's name, and the contacts database helps autocomplete so that the corresponding emails show up in the "To" field.

I've included the following files:

  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

My jQuery code in document.load is below:

   $(function() {
    function log( message ) {
        $('#to').append(message);
        console.log(message);
    }

   $( "#search" ).autocomplete({
        source: "search.php",
        minLength: 2,
        select: function( event, ui ) {
     log( ui.item? ui.item.email : "NO" );
        }
    }); 
});

And my HTML is:

<div class='ui-widget'><input type='text' class='medium' id='search' /></div> <br />
To:<br />
<div class='ui-widget'> <textarea type='text' style='width:80%; height:24px;' id='to' class='ui-widget-content'></textarea></div>

The result of search.php is fine as far as json is considered, here's a sample of the output when the letters "Ahmed" are pressed:

[{"email":"saddi@yahoo.com","name":"Ahmed Qasim"},{"email":"aaaab@alangari.com.sa","name":"Ahmed Abbas"},{"email":"mokhlef@yahoo.com","name":"Ahmed Sahdi"}]

I know I'm getting this response from search.php because I check Firebug and see it, but it doesn't show up below the search input field... Instead, what does show up is just a stump of a list... as in the image below.

enter image description here

But this exact same thing worked as expected when I used the demo code here: demo Why doesn't the list show up properly? Is there a limit on how much data can be displayed? I've pasted only 3 entries from the JSON output I got, but there were tens.

  • 写回答

2条回答 默认 最新

  • duan1226 2013-01-06 17:14
    关注

    What you are returning from server contains email and name fields, jquery ui needs an value and label field.

    [{"label":"Ahmed Abbas", value: "Ahmed Abbas"}]
    

    label is what you see on autocomplete list, and value is the value you will get when you select an item.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算