页面重新加载时触发自动完成功能

时间:2014-06-18 06:46:08

标签: c# javascript jquery asp.net autocomplete

我有一个包含多个自动填充字段的搜索表单。

当页面第一次加载时,一切都运行良好,但如果我重新加载/按F5,即使没有得到或有焦点,所有minLength:0的字段都会被触发。

没有其他行动导致这种情况。

function autocompleter(searchfield, autocompletee) {
$(searchfield)
    .autocomplete({
        minLength: autocompletee.minLength,
        source: autocompletee.array,
        change: function(event, ui) {
            if (!ui.item) {
                event.target.value = "";
            }
        },
        select: function (event, ui) {
            var chosenWord = ui.item.label;
            var searchKeyWord = {
                "webformField": autocompletee.selected,
                "searchString": chosenWord
            };
            $.ajax({
                contentType: "application/json; charset=utf-8",
                type: 'POST',
                url: "MyHandler.ashx",
                data: JSON.stringify(searchKeyWord),
            });
            var title = ui.item.label; //get the object title here
            $(searchfield).val(title);
            $(autocompletee.chosen).append("<asp:HyperLink href='#' class='myClass' runat='server' ID='" + chosenWord + "'>" + chosenWord + "</asp:HyperLink>"); // 
            $(searchfield).val("");
            $(searchfield).blur();
            return false;
        }
    }).focus(function() {
        $(this).autocomplete("search", "");
    });
}

$(document).ready(function() {

var myObject = {
    array: window.myarray,
    selected: "object_chosen",
    chosen: "#object_chosen",
    minLength: 0,
};

autocompleter('#inputField, myObject);
});


 <input type="text" id="inputField" placeholder="Text" runat="server" clientidmode="Static" />

 <asp:Panel ID="object_chosen" CssClass="chosen-tag-container" ClientIDMode="Static" runat="server">  
<%-- links appear here--%>
</asp:Panel>

1 个答案:

答案 0 :(得分:0)

试试这个

$(document).ready(function() {

var myObject = {
    array: window.myarray,
    selected: "object_chosen",
    chosen: "#object_chosen",
    minLength: 0,
};

autocompleter("#inputField", myObject); //Remove this because it's calling that function on page load
});