【发布时间】: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