我使用php從mysql數據庫中獲取數據,然後在jquery ui自動完成上使用該數據。但它看起來像返回的JSON不符合jQuery UI的需要。 的JSON返回看起來是這樣的:jquery ui自動完成和php json字符串
["dyes miran famint","annie ferrer mendrez","annie ferrer mendrez","anton setra masgre","anton setra masgre","jeniffer hades cruz","jeniffer hades cruz","alvin louie urbano maranon","alvin louie urbano maranon","francisza jerrielleza bullonza","blaze br tags anchor"]
我甚至用jQuery.parseJSON就可以了。
這裏我就是這樣做的php文件:
$class_code = $_POST['class_code'];
$student_list = array();
$students = mysql_query("SELECT accounts.id, fname, mname, lname, classcode
FROM account_details
LEFT JOIN accounts ON account_details.id = accounts.id
LEFT JOIN class_rooster ON accounts.id = class_rooster.id
WHERE accounts.status = 1 AND accounts.type='student'
AND (class_rooster.id IS NULL OR classcode !='$class_code')");
$students_num = mysql_num_rows($students);
if($students_num > 0){
while($row = mysql_fetch_assoc($students)){
$student_list[] = $row['fname'] . ' ' . $row['mname'] . ' ' . $row['lname'];
}
echo json_encode($student_list);
下面是我得到的錯誤:
Uncaught TypeError: Property 'source' of object #<Object> is not a function
這裏的JavaScript的:
function get_students(){
var class_code = $('#current_classes').val();
$.post('student_list.php', {'class_code' : class_code},
function(data){
return data;
}
);
}
$("#student_name").live('click', function(){
$("#student_name").autocomplete({
source: get_students()
});
});
我也可能錯了,但我認爲它預計的對象與ID和value屬性的數組,不只是字符串數組。 – slash197 2012-03-14 16:09:51
@pinkypower - 一串字符串將根據文檔工作:http://jqueryui.com/demos/autocomplete/#remote PHP和生成的JSON看起來很好,問題可能在您的Javascript的某處。你可以發佈嗎? – zim2411 2012-03-14 16:11:09
是的,你是對的,只是自己檢查:) – slash197 2012-03-14 16:12:29