2016-11-15 175 views
1

當前正在實現jQuery自動完成,數據從php文件傳回並填充字段,但自動完成下拉列表中沒有任何文本。您可以選擇條目jQuery自動完成數據

圖片:enter image description here

好像對結果的文本沒有被傳遞迴?

try { 
    $stmt = $db->prepare('SELECT id, firstname, surname, department, email FROM users WHERE firstname LIKE :term'); 
    $stmt->execute(array('term' => '%'.$_GET['term'].'%')); 

    while($row = $stmt->fetch()) { 
     $row_array['id'] = $row['id']; 
     $row_array['firstname'] = $row['firstname']; 
     $row_array['surname'] = $row['surname']; 
     $row_array['email'] = $row['email']; 
     $row_array['department'] = $row['department']; 

     $return_arr[] = $row_array; 

    } 

} catch(PDOException $e) { 
    echo 'ERROR: ' . $e->getMessage(); 
} 


/* Toss back results as json encoded array. */ 
echo json_encode($return_arr); 
} 

//jQuery 
$(document).ready(function() { 
$(".auto").autocomplete({ 
    source: "search.php", 
    minLength: 1, 
    datatype: 'json', 
    select: function(event, ui) { 
     $('#firstname').val(ui.item.firstname) 
     $('#surname').val(ui.item.surname); 
     $('#department').val(ui.item.department); 
     $('#email').val(ui.item.email); 
     $('#hidden').val(ui.item.id); 
    } 
}); 
}); 

回答

0

如文檔中所述的,在它的最簡單的形式,你的JSON應答可以是和字符串的數組:

[ "Choice1", "Choice2" ] 

它也可以是含有一個標籤對象的數組和一個值,以顯示在自動完成字段,例如:

[ { label: "Choice1", value: "value1" }, ... ] 

事實上,數據更要返回結構不能被JQuery用戶界面自動完成插件處理。

見詳細的文件位置:http://api.jqueryui.com/autocomplete/#option-source

希望這有助於。