How to Map data in jquery-ui autocomplete is not working

流过昼夜 提交于 2019-12-05 11:52:56

You have defined your dataType as jsonp but it looks like you are returning standard json, try changing your dataType:

$("#search").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Home/GetCompanyNames",
            dataType: "json",
            data: "searchterm=" + request.term,
            success: function (data) {
                response($.map(data, function (item) {
                    return {
                        label: item.Name,
                        value: item.Name
                    };
                }));
            }});
    },
    minLength: 2,
    select: function (event, ui) {
        log(ui.item ? "Selected: " + ui.item.label : "Nothing selected, input was " + this.value);
    },
    open: function () {
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});

im not sure what is wrong in ur code but may be u can try another more simple way to do autocomplete? Like

$(document).ready(function () {
    $(":input[dataautocomplete]").each(function () {
        $(this).autocomplete({
            source: $(this).attr("dataautocomplete"),
            minLength: 2,
            select: function (event, ui) {
                //do anything u need
                //get ur name  ui.item.Name
                //get ur URL   ui.item.LogoUrl
            }
        });
    });
}); 

html like

<input  class="ui-autocomplete-input" type="text" value="" name="post" dataautocomplete="@Url.RouteUrl("default", new { controller = "somecontroller", action = "someaction" })" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!