【问题标题】:Jquery UI Autocomplete This not working to pass id as separate parameterJquery UI Autocomplete 这不能将 id 作为单独的参数传递
【发布时间】:2012-08-15 11:21:29
【问题描述】:

所以我试图传递另一个参数“field”,它可以让我获取正在输入的输入的 id。这将允许我指导 PHP 向数据库发出正确的 SQL 语句,就像我将在需要自动完成功能的页面上有多个输入。我知道下面缺少响应,那是因为我不确定该放什么。我只想像平常一样返回 json 字符串。我怎样才能获得要传递的 id 以及我为响应输入的内容?

('.completeme').autocomplete({    
            source: function(request, response) {
                $.ajax({
                    url: "ajax/ajax_diagnosis.php",
                    datatype: "json",
                    data: {
                        term: request.term,
                        field: $(this).attr("id"),
                        maxRows: 15                     
                }
            },

            minLength: 3,
        });

【问题讨论】:

    标签: jquery ajax json autocomplete


    【解决方案1】:

    尝试:

    $(this.element).attr("id")
    

    这可能是最终脚本:

    $(function() {
            var cache = {},
                lastXhr={};
            $( ".completeme" ).autocomplete({
                minLength: 3,
                source: function( request, response ) {
    
                        var term = request.term,
                            field=$(this.element).attr('id');
                        if(!field)return;
                        if ( cache[field] && term in cache[field] ) {
                            response( cache[field][ term ] );
                            return;
                        }
    
                    $.extend(request,{field:field,maxRows:15});
                    lastXhr[field] = $.getJSON( "ajax/ajax_diagnosis.php", 
                                        request, 
                                        function( data, status, xhr ) {
                                        if(!cache[field])cache[field]={};
                                                  cache[field][ term ] = data;
                                                  if ( xhr === lastXhr[field] ) {
                                                    response( data );
                                                 }
                                    });
                }
            });
        });
    

    【讨论】:

    • 我可以看到这确实传递了 id(查看了 firebug 以查看已发送的请求)但是我仍然不确定如何正确执行成功参数。我已经尝试了好几个小时,但无济于事。有什么建议吗?
    • 难以置信。这工作得很好......如果我知道每行做了什么......
    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多