简体   繁体   English

JSON自动完成,不显示结果

[英]JSON autocomplete, doesn't show results

I am trying to implement JSON for the first time, for an autocomplete input type. 我正在尝试为自动完成输入类型首次实现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")" />

}

But it is complaining that data-autocomplete-source is not a valid attribute. 但是,它抱怨data-autocomplete-source不是有效的属性。 It goes into the QuickSearch but I don't see a autocomplete result. 它进入了快速搜索,但没有看到自动完成结果。

 target.data("autocomplete-source");

use the data attribute. 使用数据属性。 jQuery. jQuery的。 data 数据


Replace: 更换:

$("input[data-autocomplete-source]").each(function () {
    var target = $(this);
    target.autocomplete({ source: target.attr("data-autocomplete-source") });
});

with: 与:

$(function () {
    $("input[data-autocomplete-source]").each(function () {
        var target = $(this);
        target.autocomplete({ source: target.data("autocomplete-source") });
    });
});

You using $(function () {}) to wait until the page is "ready" and the element exists. 您使用$(function () {})等待页面“就绪”并且元素存在。


Change: 更改:

<input type="text" name="q" data-autocomplete-source="@Url.Action("QuickSearch", "Search")" />

to: 至:

<input class="my-autocomplete" type="text" name="q" data-autocomplete-source="@Url.Action("QuickSearch", "Search")" />

And change: 并更改:

$("input[data-autocomplete-source]").each

to: 至:

$("input.my-autocomplete").each

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM