2011-09-01 77 views
0

你好我使用ajax autocomplete for jquery插件如何编辑该代码在AJAX自动完成的jQuery

http://www.devbridge.com/projects/autocomplete/jquery/

有一个代码

function Autocomplete(el, options) { 
    this.el = $(el); 
    this.id = options.id; 
    this.el.attr('autocomplete', 'off'); 
    this.suggestions = []; 
    this.data = []; 
    this.badQueries = []; 
    this.selectedIndex = -1; 
    this.currentValue = this.el.val(); 
    this.intervalId = 0; 
    this.cachedResponse = []; 
    this.onChangeInterval = null; 
    this.ignoreValueChange = false; 
    this.serviceUrl = options.serviceUrl; 
    this.isLocal = false; 

    //this.options.fnFormatResult = fnFormatResult(); 

    this.options = { 
     autoSubmit: false, 
     minChars: 1, 
     maxHeight: 300, 
     deferRequestBy: 0, 
     width: 0, 
     highlight: true, 
     params: {}, 
     fnFormatResult: fnFormatResult,  
     delimiter: null, 
     zIndex: 9999 
    }; 
    this.initialize(); 
    this.setOptions(options); 
    } 

在那里

this.options = { 
    autoSubmit: false, 
    minChars: 1, 
    maxHeight: 300, 
    deferRequestBy: 0, 
    width: 0, 
    highlight: true, 
    params: {}, 
    fnFormatResult: fnFormatResult,  
    delimiter: null, 
    zIndex: 9999 
}; 

它有定义一个函数来格式化结果

fnFormatResult: fnFormatResult, 

我想使用fnFormatResult之外的其他功能,例如fnFormatResult2

我想调用函数acording传递一个参数更改为插件

我需要做这样的

if(param == 1){ 
fnFormatResult: fnFormatResult, 
} 
else if(param == 1){ 
fnFormatResult: fnFormatResult2, 
} 

我怎么能做到这一点。请帮助......................

+0

我不能帮你这个,但我知道你可以做你想要的与jquery ui自动完成 – kmcc049

回答

2

嗯,我想你可以这样做:

this.options = { 
    autoSubmit: false, 
    minChars: 1, 
    maxHeight: 300, 
    deferRequestBy: 0, 
    width: 0, 
    highlight: true, 
    params: {}, 
    fnFormatResult: function(value, data, currentValue) { 
    if(param==1) { 
     return fnFormatResult.call(this, value, data, currentValue); 
    } else if (param==2) { 
     return fnFormatResult2.call(this, value, data, currentValue); 
    } 
    },  
    delimiter: null, 
    zIndex: 9999 
}; 
+0

这个工程。非常感谢你.................:D:D –

1

我觉得你可以做一些修改in功能:

function fnFormatResult(value, data, currentValue) { 
    var pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')'; 
    return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>'); 
    } 

您可以在这个函数中实现两个定义。