jQuery UI 自动完成 JSON 响应问题

发布于 2024-12-05 21:20:30 字数 1550 浏览 0 评论 0原文

我已经做了很多研究,但我仍然不知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-12-12 21:20:30

看起来您正在执行 post 然后尝试在 php 页面上获取 $_GET['tags'] 。因此,尝试使用 ajax 函数并在 php 页面上选择 $_GET['term'] 。另外,$.maps 不是一个函数。我认为你的意思是$.map

而且,如果您希望将 label 作为输入的值,则不要指定 value 字段。如果您只指定一个,自动完成功能将为两者使用label

jQuery 自动完成:

$("#tags").autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "/panel/tags.php",
            dataType: "json",
            data: {term: request.term},
            success: function(data) {
                    response($.map(data, function(item) {
                        return {
                            label: item.tagName
                       };
                }));
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
      /* Do something with user_id  */
    }
});

PHP:

$query_tags = "SELECT tagID, tagName FROM DCB_Tags WHERE tagName LIKE '".mysql_real_escape_string($_GET['term'])."%' ORDER BY tagName ASC LIMIT 10";

链接到教程:

It looks like you are doing a post then trying to pick up $_GET['tags'] on the php page. So, try using an ajax 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 a value field. The autocomplete will use label for both if you only specify one.

jQuery autocomplete:

$("#tags").autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "/panel/tags.php",
            dataType: "json",
            data: {term: request.term},
            success: function(data) {
                    response($.map(data, function(item) {
                        return {
                            label: item.tagName
                       };
                }));
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
      /* Do something with user_id  */
    }
});

PHP:

$query_tags = "SELECT tagID, tagName FROM DCB_Tags WHERE tagName LIKE '".mysql_real_escape_string($_GET['term'])."%' ORDER BY tagName ASC LIMIT 10";

Link to tutorial: http://www.jensbits.com/2011/08/24/using-jquery-autocomplete-when-remote-source-json-does-not-contain-label-or-value-fields/

终陌 2024-12-12 21:20:30

我输入“警报(数据长度);”在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文