當使用jQuery自動完成時,我得到[object object]在自動完成元素下面呈現,而不是數據庫中的值(錯誤截圖爲click here)。有人可以建議我去哪裏錯了。Jquery AutoComplete返回[對象對象]
這裏是我的jQuery Mobile的代碼
$(document).on("pageinit", "#myPage", function() {
$("#autocomplete").on("listviewbeforefilter", function (e, data) {
var $ul = $(this),
$input = $(data.input),
value = $input.val(),
html = "";
$ul.html("");
if (value && value.length > 2) {
$ul.html("<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>");
$ul.listview("refresh");
$.ajax({
url: "/get_names/",
dataType: "json",
crossDomain: true,
data: {
q: $input.val()
}
})
.then(function (response) {
$.each(response, function (i, val) {
html += "<li>" + val + "</li>";
});
$ul.html(html);
$ul.listview("refresh");
$ul.trigger("updatelayout");
});
}
});
});
這裏是我的html代碼
<div data-role="page" id="myPage">
<div data-role="content">
<ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Search House Id..." data-filter-theme="d"></ul>
</div><!-- /content -->
</div>
這裏是views.py
def get_names(request):
q = request.GET.get('term', '')
names = Names.objects.filter(names__startswith=q)[:10]
results = []
if names.count > 0:
for name in names:
name_json = {}
name_json['id'] = name.id
name_json['label'] = name.name
name_json['value'] = name.name
results.append(name_json)
data = json.dumps(results)
else:
data = 'fail'
mimetype = 'application/json'
return HttpResponse(data, mimetype)
謝謝,它的工作原理。 – kurrodu