遇到Jquery UI自动完成问题

时间:2011-09-14 05:21:19

标签: php jquery mysql jquery-ui mysqli

我的php代码看起来像那样

if (isset($_REQUEST['term']))
{
    $term = trim(strip_tags($_REQUEST['term']));//retrieve the search term that autocomplete sends

    $result = $db->query("SELECT company as value,id FROM main WHERE company LIKE '$term'") or die(mysqli_error());;
    $results = array();
    while ($row = $result->fetch_row()) $results[] = array( 'id' => $row[0] , 'label' => $row[1], 'value' => $row[1] );
    echo json_encode($results);
}

下面的Js代码

$("#auto").autocomplete({
    source: "index.php",
    minLength: 2,//search after two characters
    select: function(event,ui){
    }
});

和HTML标记

<input id="auto" name="company"/>

代码有什么问题?它不会生成自动完成选项..在php日志文件中没有错误。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

假设json没问题,那么你没有对你得到的数据做任何事情

select: function(event,ui){
// do something with the data you get back
//you would usually have a function here to do something with data
// but this sample should be enough for you 
var id = ui.id;  // id you got back assign to var
var label = ui.label; // label you got back assign to var
$('#gothisback').val(ui.id); //push it to a div
    }