[英]JSON autocomplete, doesn't show results
我正在嘗試為自動完成輸入類型首次實現JSON。
@{
ViewBag.Title = "Index";
}
<script type="text/javascript">
function searchFailed(){
$("#searchresults").html("Sorry, there was a problem with the search.");
}
$("input[data-autocomplete-source]").each(function () {
var target = $(this);
target.autocomplete({ source: target.attr("data-autocomplete-source") });
});
</script>
<h2>Index</h2>
@using (Ajax.BeginForm("QuickSearch", "Search", new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", OnFailure = "searchFailed", LoadingElementId = "ajax-loader", UpdateTargetId = "searchresults", }))
{
<input type="text" name="q" data-autocomplete-source="@Url.Action("QuickSearch", "Search")" />
}
但是,它抱怨data-autocomplete-source不是有效的屬性。 它進入了快速搜索,但沒有看到自動完成結果。
target.data("autocomplete-source");
使用數據屬性。 jQuery的。 數據
更換:
$("input[data-autocomplete-source]").each(function () {
var target = $(this);
target.autocomplete({ source: target.attr("data-autocomplete-source") });
});
與:
$(function () {
$("input[data-autocomplete-source]").each(function () {
var target = $(this);
target.autocomplete({ source: target.data("autocomplete-source") });
});
});
您使用$(function () {})
等待頁面“就緒”並且元素存在。
更改:
<input type="text" name="q" data-autocomplete-source="@Url.Action("QuickSearch", "Search")" />
至:
<input class="my-autocomplete" type="text" name="q" data-autocomplete-source="@Url.Action("QuickSearch", "Search")" />
並更改:
$("input[data-autocomplete-source]").each
至:
$("input.my-autocomplete").each
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.