在文本框焦点上填充smartautocomplete

时间:2015-04-06 11:18:49

标签: asp.net-mvc autocomplete

我正在使用smartautocomplete。当我搜索任何数据并且在滚动上完美地工作时,它工作得很好。 现在我正在尝试当用户点击文本框然后它将默认显示自动完成框但我无法在焦点上打开默认自动完成。 我正在使用smartautocomplete.js。 我的代码如下:

 $(document).ready(function () {
    //Input for testing purposes
    $("#inp").smartautocomplete({
        getDataFunc: getData,
        pageSize: 15,
        autoFocus: true,
        focus: function (event, ui) {
            event.preventDefault();
        },
        select: function (event, ui) {
            event.preventDefault();
            $('#inp').val(ui.item.label);
            $('#inp1').val(ui.item.value);
            //$('#<%=hdnUserID.ClientID%>').val(ui.item.id)
        },
        change: function (event, ui) {
        }
    }).focus(function () {
       //what to write here so i can get default autocomplete value on focus
    });
});


//Function the SA plugin called when data is needed. 
//var getData = function (input, pageIndex, pageSize, callback) {
var getData = function (input, pageIndex, pageSize, callback) {
    //In this example I use a WebMethod, but you can call anything from a local source to any web service.
    //url: "../../../Base/BindStateMaster?pageSize=10&pageNum=1&CountryCode=" + ParentddlValue,
    $.ajax({
        type: "POST",
        url: "../../../Base/BindCountryMasterAuto",
        data: "{'input':'" + input + "','pageIndex':'" + pageIndex + "','pageSize':'" + pageSize + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        dataFilter: function (data) { return data; },
        success: function (response) {

            if (response) {
                //alert(JSON.stringify(response));
                //Data is assumed to be received in a {label: , value: , ...} form, as needed by jqueryUI autocomplete. Of course, if you change the _renderItem function, you are free to modify this as you want
                response = $.map(response.Data, function (item) {
                    return {
                        label: item.text,
                        value: item.id
                    }
                });
                callback(response);
            }
            else callback();
        }

    });


}

1 个答案:

答案 0 :(得分:0)

你可以试试这个

    $(function() {
        $('#id').autocomplete({
            source: ["your source array",

                    ],
            minLength: 0
        }).focus(function(){            
            $(this).trigger('keydown.autocomplete');
// or $(this).autocomplete("search", $(this).val());
        });
    });