我正在創建一個使用jquery自動完成的搜索欄。jquery自動完成不顯示文本框中的選定值
這裏是我的自動完成代碼:
<script type="text/javascript">
$(document).ready(function() {
$(function() {
$(".txtSearch").autocomplete(
{
source: function (request, response) {
$.ajax({
url: '@Url.Action("AutoComplete", "components")',
type: "GET",
data: request,
success: function (data) {
response($.map(data, function (el) {
return {
label: el.autoSuggest,
value: el.resultCount
};
}));
}
});
},
minLength: 3,
select: function (event, ui) {
$(this).val(ui.item.label);
var values = ui.item.label;
$.ajax({
type: 'GET',
contentType: 'application/json',
cache: false,
url: '@Url.Action("SearchFunc", "components")',
dataType: 'json',
data: { searchTerm: values }
}).done(function (result) {
window.location.href = "search.aspx?pass=" + encodeURIComponent(result);
})
}
}).data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append("<a><table width='100%'><tr><td><strong>" + item.label + "</strong></td><td align='right' style='font-size: 10px;'>" + item.value + "</td></tr></table></a>")
.appendTo(ul);
};
});
});
</script>
例如,如果有人開始輸入「市」,自動完成顯示輸出是這樣的:
shirts 2results
tshirts 3results
我的問題是,當我選擇任何汽車 - 推薦選項,文本框只顯示值,而不顯示標籤。
例如在上述情況下,如果我選擇襯衫,文本框顯示2件結果。 但我的第二個Ajax函數傳遞的參數是正確的,即襯衫
任何人都可以提出解決方案嗎?
你的代碼工作。但是,當我使用箭頭鍵,它仍然顯示值.... – nitinvertigo
箭頭鍵觸發焦點事件,所以你也需要取消。 –
是的,我已經做到了......發佈了答案..感謝您的幫助 – nitinvertigo