Jquery ui JQuery自动完成源代码是一个函数

Jquery ui JQuery自动完成源代码是一个函数,jquery-ui,jquery-ui-autocomplete,Jquery Ui,Jquery Ui Autocomplete,我正在为不同的字段使用jQueryUIAutoComplete。为了获取数据,我使用函数作为源。它工作得很好! 我想知道是否有一种方法不在源代码中使用匿名函数,而是声明一个通用函数,该函数将有一个参数重定向到正确的URL。 我对JS和JQuery很陌生,所以我不知道匿名函数中的参数请求和响应是什么。 以下是我想做的: $ac.autocomplete({ //Call the function here, but what are the parameter reques

我正在为不同的字段使用jQueryUIAutoComplete。为了获取数据,我使用函数作为源。它工作得很好! 我想知道是否有一种方法不在源代码中使用匿名函数,而是声明一个通用函数,该函数将有一个参数重定向到正确的URL。 我对JS和JQuery很陌生,所以我不知道匿名函数中的参数请求和响应是什么。 以下是我想做的:

    $ac.autocomplete({
        //Call the function here, but what are the parameter request and response???
        source: autocomplete(),
        minLength: 1
    });
这是我想调用的函数

function autoComplete(request, response, url) {
    $.ajax({
        url: '/Comp/'+url,
        dataType: "json",
        type: "POST",
        success: function (data) {
            response($.map(data, function(item) {
                return { label: item, value: item, id: item };
            }));
        }
    });
}
非常感谢您的帮助。

您应该使用

source: autoComplete
而不是

source: autocomplete()

还有一句话。jQuery UI自动完成的默认实现仅使用
标签
,而不使用
id

重新格式化您的问题将构成问题的解决方案。:)

jqueryui需要请求和响应对象。request.term将为您提供用户键入的文本,response方法将标签和值项返回小部件工厂,以显示建议下拉列表


p.S:假设您的JSON字符串包含名字键

我将举一个发生在我身上的情况的例子,可以作为一个例子:

情况:用户使用Jquery自动完成选择关键字后,不允许将其列出。考虑到查询的执行是相同的,即未修改的cat。服务器端

代码如下所示:

$( "#keyword-search" ).autocomplete({
    minLength: 3 ,
    source: function( request , response ) {
        var param = { keyword_type: type , keyword_search: request.term } ;
        $.ajax({
            url: URI + 'search-to-json',
            data : param,
            dataType: "json",
            type: "GET",
            success: function (data) {
                response($.map(data, function( item ) {
                    /* At this point I call a function, I use to decide whether to add on the list to be selected by the user. */
                    if ( ! is_record_selected( item ) ) {
                        return item;
                    }
                }));
            }
        });
    } ,
    select: function( event , ui ) {
        /* Before you add, looking if there is any cell */
        /* If it exists, compare the id of each cell not to add an existing */
        if ( validate_new_keyword( ui ) ) {
            add_cell( ui ) ;
        }
    } ,
});

/* Any validation... */
function validate_new_keyword( ui ) {
    var keyword_id = $.trim(ui.item.id) ;
    Any condition...
    if (keyword_id > 0) {
        return true ;           
    }

    return false ;
}

/* This function checks if a keyword has not been selected by the user, it checks for the keyword_array. */
function is_record_selected( item ) {
    var index = jQuery.inArray( item.id , keyword_array ) ;
    return index == -1 ? false : true;
}

Obs:因此可以在“源”和“选择”中使用函数=p

ID是一个默认字段,请参见此处的示例源代码:@FelixEve:对不起,我不同意你的观点。仅描述服务器响应的两种标准表示形式:字符串数组和具有
标签
属性的项数组。如果这些项有一些其他自定义属性,那么这些属性将被保存,但jQuery UI自动完成不会以任何方式使用。例如,在渲染或选择过程中,必须覆盖方法才能使用属性,如
id
$( "#keyword-search" ).autocomplete({
    minLength: 3 ,
    source: function( request , response ) {
        var param = { keyword_type: type , keyword_search: request.term } ;
        $.ajax({
            url: URI + 'search-to-json',
            data : param,
            dataType: "json",
            type: "GET",
            success: function (data) {
                response($.map(data, function( item ) {
                    /* At this point I call a function, I use to decide whether to add on the list to be selected by the user. */
                    if ( ! is_record_selected( item ) ) {
                        return item;
                    }
                }));
            }
        });
    } ,
    select: function( event , ui ) {
        /* Before you add, looking if there is any cell */
        /* If it exists, compare the id of each cell not to add an existing */
        if ( validate_new_keyword( ui ) ) {
            add_cell( ui ) ;
        }
    } ,
});

/* Any validation... */
function validate_new_keyword( ui ) {
    var keyword_id = $.trim(ui.item.id) ;
    Any condition...
    if (keyword_id > 0) {
        return true ;           
    }

    return false ;
}

/* This function checks if a keyword has not been selected by the user, it checks for the keyword_array. */
function is_record_selected( item ) {
    var index = jQuery.inArray( item.id , keyword_array ) ;
    return index == -1 ? false : true;
}