【发布时间】:2014-12-04 06:41:50
【问题描述】:
我正在查看此页面上的示例源代码(粘贴在下面):
http://jqueryui.com/autocomplete/#remote
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Remote datasource</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.ui-autocomplete-loading {
background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat;
}
</style>
<script>
$(function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#birds" ).autocomplete({
source: "/search",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds">
</div>
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>
</body>
</html>
我的服务器返回的 json 对象应该是什么样的?现在是:
{source1:[],source2:[]}
我返回多个来源,并希望以如下示例所示的分类格式显示数据:http://jqueryui.com/autocomplete/#categories
首先,我只是想显示数据源。
【问题讨论】:
-
你的 ajax 应该像示例
[ { label: "anders", category: "" }, { label: "andreas", category: "" }, { label: "antal", category: "" }, { label: "annhhx10", category: "Products" }, { label: "annk K12", category: "Products" }, { label: "annttop C13", category: "Products" }, { label: "anders andersson", category: "People" }, { label: "andreas andersson", category: "People" }, { label: "andreas johnson", category: "People" } ]; -
这使得结果出现了! :) 但它不显示类别!它将它们全部显示为一个列表。如何让它们分成两个列表?喜欢:jqueryui.com/autocomplete/#categories
-
如果你会做小提琴会很有帮助
-
我不明白你的意思。我只是使用示例中的代码。 XD
-
我建议您阅读这篇博客文章,其中包含相关示例。 salman-w.blogspot.in/2013/12/…
标签: javascript jquery json jquery-ui autocomplete