且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery可重用的自动完成功能

更新时间:2023-11-21 17:41:34

您应该可以将其添加到autocomplete调用的末尾:

You should be able to add it to the end of the autocomplete call:

function Autocomplete(numberLocation,nameLocation,dscLocation,chargeLocation) {
    $(fundNumberField).autocomplete ({
        minLength: 4, 
        source: function(request, response){             
            var fundnum = $('#str-misc-FundNumber1').val(); 
            fundnum = escape(fundnum); 
            var querystring = "?term=" + fundnum; 
            if (typeof (window.sortorder)=='undefined'){
                querystring = querystring + '&sortorder=0';
            } else {
                querystring = querystring + '&sortorder=' + window.sortorder;                
            }
            $.ajax({
                url:      'ajax/fundid-autocomplete.php'+querystring,
                beforeSend: function(){},
                async:    true,
                dataType: "json",
                success: response
            });
        },
        focus: function ( event,ui ){     
            $(numberLocation).val( ui.item.value );
            return false;            
        },
        select: function( event, ui ) {          
                $(numberLocation).val( ui.item.value );
                $(nameLocation).html( ui.item.desc );                     
                    if(ui.item.dsc >0) {
                        chargeLocation.hide();                    
                        dscLocation.show();                          
                        dscLocation.html('DSC Charge: '+ui.item.dsc+' %');
                    } else {
                        dscLocation.html('');                    
                        chargeLocation.show();                       
                    }  
                    $('#numberlabel').html('Fund #*');
                return false;
        }        
    }).data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
                .data( "item.autocomplete", item )
                .append( "<a>" + item.value + " | " + item.desc + "</a>" )
                .appendTo( ul );
    };         
}

当我通过JSLint运行该代码时,我实际上没有任何问题(我确实在source函数中添加了一个缺少的分号).唯一缺少的是fundNumberField定义(我假设是在其他地方定义的).

When I run that code through JSLint I don't really have any issues (I did add a missing semicolon inside the source function). The only thing that's missing is the fundNumberField definition (I'm assuming that's defined elsewhere).