如何使用jquery自动完成消息应用程序的用户名?

我正在玩一个小消息框,我正在尝试使用jquery UI进行自动填充function,但它无法正常工作。 没有自动填充。 这是我到目前为止所得到的……

composemessage.php

 $(function() { $( "#tags" ).autocomplete({ source: "usersarray.php", dataType:'json', minLength: 0, delay:0 }); $( "#tags" ).click(function() { $( "#tags" ).autocomplete("search",""); }); });  

usersarray.php

 $id = $_SESSION['account_id']; $getmsg = "SELECT * FROM user WHERE account_id = $id"; $showmsg = @mysqli_query ($dbc, $getmsg); while ($row = mysqli_fetch_array($showmsg, MYSQLI_ASSOC)) { $arrResults = array($row['user_username']); } // END WHILE // Print them out, one per line echo json_encode($arrResults); 

JSON结果应如下所示:

 users = ['user1', 'user2']; 

根据您的jquery请求,您期望以该格式提供某些内容。 你能告诉我们你的usersarray.php文件的输出是什么吗? 如果用户名对您很重要,请不要忘记屏蔽用户名。

Json必须采用以下格式:

  [{label: 'label1', value: 'value1'},{label: 'label2', value: 'value2'},{label: 'label3', value: 'value3'}]; 

javascrip部分:

 $( "#tags" ).autocomplete({ source: function( request, response ) { var term = request.term; $.ajax({ url: "username.php", dataType: 'json', async: true, data: request, timeout: 3000, success: function(data,status,xhr){ response( data ); } }); }, delay: 100, minLength: 0 }); 

HTML