简体   繁体   English

jQuery UI输入自动完成,无需匿名功能

[英]jQuery UI input autocomplete without anonymous function

I managed to get jQuery UI autocomplete to work. 我设法使jQuery UI自动完成工作。 However since I use it in many input fields I wanted to make it more generic so I don't have to replicate it. 但是,由于我在许多输入字段中都使用了它,因此我想使其更通用,因此不必复制它。 So instead of the following code: 因此,而不是以下代码:

$(function() {
    var cache = {};
    $("#searchGuyInput").autocomplete({
        minLength: 2,
        source: function(request, response) {
            // Getting a JSON array to populate the list
            var term = request.term;
            if (term in cache) {
                response(cache[ term ]);
                return;
            }

            $.getJSON("c_select_guy.php", {request: term}, function(data, status, xhr) {
                cache[ term ] = data;
                response(data);
            });
        },
        select: function(event, ui) {
            // What happens once I have selected a name from the list
            if (ui.item){
                createInputField(ui.item.Name + " " + ui.item.Surname,ui.item.guyID);
            }
        }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        // Here I can format the JSON array to display the rows in the list
        return formatted;
    };

    function createInputField(message, guyID) {
        // creates a new div for the selected element
    };    
});

I tried to use: 我尝试使用:

function autocomp(inputID) {  //CHANGED
    var cache = {};
    $(inputID).autocomplete({ //CHANGED
        minLength: 2,
        source: function(request, response) {
            // Getting a JSON array to populate the list
            var term = request.term;
            if (term in cache) {
                response(cache[ term ]);
                return;
            }

            $.getJSON("c_select_guy.php", {request: term}, function(data, status, xhr) {
                cache[ term ] = data;
                response(data);
            });
        },
        select: function(event, ui) {
            // What happens once I have selected a name from the list
            if (ui.item){
                createInputField(ui.item.Name + " " + ui.item.Surname,ui.item.guyID);
            }
        }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        // Here I can format the JSON array to display the rows in the list
        return formatted;
    };

    function createInputField(message, guyID) {
        // creates a new div for the selected element
    };    
}

autocomp("#searchGuyInput"); //CHANGED

I highlighted the 3 changes with //CHANGED comments for you. 我用// CHANGED注释为您突出显示了3个更改。

However now even though the autocomplete returns me a nice populated list of elements, when I select from the elements in the autocomplete the ui.item variable in the select function is undefined and it doesn't work anymore! 但是现在,即使自动完成功能为我返回了一个很好的填充元素列表,当我从自动完成功能中的元素中进行select时, select函数中的ui.item变量未定义并且不再起作用了!

What am I understanding wrong? 我理解错了什么?

I thought the $(function()); 我以为$(function()); was just a way of defining an anonymous function in javascript and hence if I made it named I could call it multiple times for different input fields. 只是在javascript中定义匿名函数的一种方式,因此,如果我将其命名,则可以针对不同的输入字段多次调用它。

Found the soluction in this thread: How to create generic (reusable) JavaScript autocomplete function 在此线程中找到了解决方案: 如何创建通用(可重用)JavaScript自动完成功能

Apparently calling it as 显然称它为

autocomp("#searchGuyInput");

doesn't work and you need to call it as: 不起作用,您需要将其称为:

$(function() {
        autocomp("#searchGuyInput");
});

I still don't get what these $(function() calls do so if anyone wants to clarify it I would be very glad. 我仍然不了解这些$(function()调用的作用,因此,如果有人想澄清一下,我将非常高兴。

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

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