【问题标题】:JSON value doesn't well formed in jquery autocompleteJSON 值在 jquery 自动完成中的格式不正确
【发布时间】:2012-07-03 16:02:53
【问题描述】:

我的 jquery 自动完成脚本有问题。 这是我的代码:

返回

<?php
require_once("include/global.php");
require_once("include/con_open.php");
$q = "select name, id from tbl_hotel";
$query = mysql_query($q);
if (mysql_num_rows($query) > 0) {
    $return = array();
    while ($row = mysql_fetch_array($query)) {
    array_push($return,array('label'=>$row["name"],'id'=>$row["id"]));
    }
}
echo(json_encode($return));

正面

<input type="text" id="hotel" />

<link rel="stylesheet" type="text/css" href="http://cdn0.favehotels.com/v2/style/autocomplete/jquery.ui.all.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>

<script type="text/javascript">                     
$(document).ready(function() {
    $( "#hotel" ).autocomplete({
        position: { my : "right bottom", at: "right top" },
        source: "hotel-search.php",
        minLength: 2,
        select: function( event, ui ) {
            window.location.href=ui.item.id;
        }                   
    })._renderItem = function( ul, item ) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append($("<a></a>").text(item.label))   
            .appendTo(ul);
    };
});
</script>

source: "hotel-search.php" 返回[{"label":"A", "id":"1"}]

当我将source: "hotel-search.php" 更改为source: [{"label":"A", "id":"1"}]

还不行。

但当我将其更改为 source: [{label:"A", id:"1"}] 时,它工作正常。

我应该怎么做才能使“hotel-search.php”的返回类似于{label:"Hotel A", id:"1"} 而不是{"label":"Hotel A", "id":"1"}

【问题讨论】:

  • 如果您的问题是关于如何修改从hotel-search.php 返回的结果,那么您需要向我们展示该页面中的代码,而不是请求页面中的代码。跨度>
  • {label:"Hotel A"} 不是有效的 JSON。我怀疑任何图书馆都会允许你这样做。顺便说一句:标准 JSON 是否有任何错误? it doesn't work 是什么意思?
  • 当你说“还不行”时,有什么问题?你有什么错误吗?
  • "it doesn't work yet" 是指自动完成继续显示所有列表。它一直显示“A”,即使我输入了“B”
  • jqueryui.com/demos/autocomplete/#remote 在演示中查看 xhr 响应...

标签: java jquery json autocomplete


【解决方案1】:

尝试$.getJSON 让 jQuery 获取 JSON 响应并将其解析为 JSON 对象:

$.getJSON('hotel-search.php', function(jsonData) {
   $("#hotel" ).autocomplete({
        position: { my : "right bottom", at: "right top" },
        source: jsonData,
        minLength: 2,
        select: function( event, ui ) {
            window.location.href=ui.item.id;
        }                   
    })._renderItem = function( ul, item ) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append($("<a></a>").text(item.label))   
            .appendTo(ul);
    };
});

【讨论】:

    【解决方案2】:

    Naufal Abu Sudais:“它还不起作用”是指自动完成功能不断显示所有列表。它一直显示“A”,即使我输入了“B”

    当您将autocomplete 与AJAX 调用一起使用时,会向服务发送一个GET 参数term

    hotel-search.php?term=WhatYouTypeInAutocomplete
    

    所以如果你想要最短的列表,你必须在你的 PHP 文件中使用$_GET['term'] 来过滤响应项。

    【讨论】:

    • 从一开始,当我使用它时它就可以工作。但如果使用该方法,自动完成需要很长时间等待后端首先生成查询。
    • 我的问题是:我应该怎么做才能使“hotel-search.php”的返回像{label:"Hotel A", id:"1"}而不是{"label":"Hotel A", "id":"1"}
    • 查看 jquery 演示并查找 JSON 响应:jqueryui.com/demos/autocomplete/search.php?term=corn
    猜你喜欢
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多