jQuery UI 自动完成 JSON 响应问题
我已经做了很多研究,但我仍然不知道如何使用 jQuery ui 自动完成功能。自动完成功能不起作用,我不知道为什么。任何帮助将不胜感激!
我在前端有以下内容。 1. jQuery正确链接。 2. jQuery-ui 正确链接。 3. jQuery-ui css 正确链接。
<script>
$("#tags").autocomplete({
source: function(request, response){
$.post("/panel/tags.php", {data:request.term}, function(data){
response($.maps(data, function(item) {
return {
label: item.tagName,
value: item.tagID
}
}))
}, "json");
},
minLength: 2,
dataType: "json",
cache: false,
focus: function(event, ui) {
return false;
},
select: function(event, ui) {
this.value = ui.item.label;
/* Do something with user_id */
return false;
}
});
</script>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" size="50" />
</div>
在后端:
$query_tags = "SELECT tagID, tagName FROM DCB_Tags WHERE tagName LIKE '".$_GET['tags']."%' ORDER BY tagName ASC LIMIT 10";
$result_tags = mysql_query($query_tags) or die ("Error: " . mysql_error());
$response = array();
$tags = array();
while($row=mysql_fetch_array($result_tags))
{
$tagID=$row['tagID'];
$tagName=$row['tagName'];
$tags[] = array('tagID'=> $tagID, 'tagName'=> $tagName);
}
header("content-type: text/javascript");
echo json_encode($tags);
exit();
输出为:
[{"tagID":"1","tagName":"艺术"},{"tagID":"4","tagName":"艺术 显示"},{"tagID":"3","tagName":"艺术家"},{"tagID":"2","tagName":"设计"}]
如果您通过输入 ?tag= 访问该页面艺术它正确地消除了“设计”。
I've done a ton of research and I still don't get how to use jQuery ui autocomplete. The auto complete does not work and I'm not sure why. Any help would be appreciated it!
I have the following on the frontend.
1. jQuery correctly linked.
2. jQuery-ui correctly linked.
3. jQuery-ui css correctly linked.
<script>
$("#tags").autocomplete({
source: function(request, response){
$.post("/panel/tags.php", {data:request.term}, function(data){
response($.maps(data, function(item) {
return {
label: item.tagName,
value: item.tagID
}
}))
}, "json");
},
minLength: 2,
dataType: "json",
cache: false,
focus: function(event, ui) {
return false;
},
select: function(event, ui) {
this.value = ui.item.label;
/* Do something with user_id */
return false;
}
});
</script>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" size="50" />
</div>
On the backend:
$query_tags = "SELECT tagID, tagName FROM DCB_Tags WHERE tagName LIKE '".$_GET['tags']."%' ORDER BY tagName ASC LIMIT 10";
$result_tags = mysql_query($query_tags) or die ("Error: " . mysql_error());
$response = array();
$tags = array();
while($row=mysql_fetch_array($result_tags))
{
$tagID=$row['tagID'];
$tagName=$row['tagName'];
$tags[] = array('tagID'=> $tagID, 'tagName'=> $tagName);
}
header("content-type: text/javascript");
echo json_encode($tags);
exit();
Output for this is:
[{"tagID":"1","tagName":"art"},{"tagID":"4","tagName":"art
shows"},{"tagID":"3","tagName":"artist"},{"tagID":"2","tagName":"design"}]
If you access the page by putting ?tag=art it correctly eliminates "design".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在执行
post
然后尝试在 php 页面上获取$_GET['tags']
。因此,尝试使用ajax
函数并在 php 页面上选择$_GET['term']
。另外,$.maps
不是一个函数。我认为你的意思是$.map
。而且,如果您希望将
label
作为输入的值,则不要指定value
字段。如果您只指定一个,自动完成功能将为两者使用label
。jQuery 自动完成:
PHP:
链接到教程:
It looks like you are doing a
post
then trying to pick up$_GET['tags']
on the php page. So, try using anajax
function and picking up$_GET['term']
on you php page. Also,$.maps
is not a function. I think you meant$.map
.And, if you want the
label
as the value of the input, then don't specify avalue
field. The autocomplete will uselabel
for both if you only specify one.jQuery autocomplete:
PHP:
Link to tutorial: http://www.jensbits.com/2011/08/24/using-jquery-autocomplete-when-remote-source-json-does-not-contain-label-or-value-fields/
我输入“警报(数据长度);”在success函数开始时,检查从服务器接收到的数据是否正确。它使得 jquery 控件在接收数据时无法显示任何内容。
I put "alert(data.length);" at the start of the success function, to check whether the data received from the server was correct. It made the jquery control fail to display anything upon receiving the data.