我想将此js自动完成代码替换为twitter typeahead

My search query has a comma. When I give it a space, its suggestion not work. So I decided to replace it with twitter typeahead. help me with code thank you or improve this code for better result ... I do not want a delay on a search query and multiple words should suggest with ignoring comma and space.

$(function () {
    $("#dept_city").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: $('#rootAddress').val()+"Searchflights/Autocomplete",  //
                dataType: "json",
                data: {
                    term : request.term,
                    q : $("#dept_city").val()
                },
                success: function(data) {
                    if(!data.length) {
                        var result = [{
                            match: 'No matches found',
                            matchr: ''
                        }];
                       response(result.slice(0, 10));
                    } else {
                        response($.map(data, function (valu) {
                            return {
                                label:valu.text, //text
                                value: valu.text,
                                country:valu.lbl,
                                city:valu.city,
                                Airportcode:valu.Airportcode,
                                airport_city:valu.airport_city,
                                id: valu.id
                            };
                        }));
                    }
                }
            });
        },
        minLength: 2,
        select: function (event, ui)
        {
            $("#dept_city").val(ui.item.label);
            $("#dept_city_hidden").val(ui.item.id);
        },
        autoFocus: true,
        search: function(){$(this).addClass('loading');},
        open: function (result) {
            $(this).removeClass('loading');
            if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
                $('.ui-autocomplete').off('menufocus hover mouseover');
            }
        },
        change: function(event, ui) {
            if (ui.item === null) {
                $("#dept_city").val("");
                $("#dept_city_hidden").val("");
            }
        }
    }).data("ui-autocomplete")._renderItem = function (ul, item) {

        if(!item.match) {
            return $("<li></li>").data("item.autocomplete", item)
            .append("<div class='dib col-md-10 col-sm-10 pad0  col-xs-10   textOverflow'><div class='mainTxt'> <span class='mainTxt_code'>" + item.country + "</span></div><span class='sr-auto-country-name'>" + item.airport_city + "</span></div><div class='col-md-2 col-sm-2 pad0  col-xs-2'><div class='fr width100'><span class='statusTag auto fr'>" + item.Airportcode + "</span></div></div>")
            .appendTo(ul);
        } else {
            return $("<li></li>")
            .data("item.autocomplete", item)
            .append("<div class='dib col-md-10 col-sm-10 pad0  col-xs-10   textOverflow'><div class='mainTxt'><span>" + item.match + "</span></div></div>")
            .appendTo(ul);
        }
    };
});
doudaotui4297
doudaotui4297 在发送之前,您可能需要对request.term进行URL编码。在这里查看更多:stackoverflow.com/questions/332872/...
12 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问