2012-07-15 61 views
1

我的功能自动完成是:jQuery的自动完成的serviceURL改变

$(document).ready(function(){ 
    var a = $('#issued_to').autocomplete(
    { 
    serviceUrl: 'http://myhost.com/ecard_emp_suggestion/', 
    minChars: 1, 
    delimiter: /(,|;)\s*/, // regex or character 
    maxHeight: 400, 
    width: 300, 
    zIndex: 9999, 
    deferRequestBy: 0, //miliseconds 
    //params: { country:'Yes' }, //aditional parameters 
    noCache: false, //default is false, set to true to disable caching 
    // callback function: 
    onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); }, 
    // local autosugest options: 
    //lookup: ['January', 'February', 'March', 'April', 'May'] //local lookup values 
    }); 
}); 

现在的问题是,它发送请求: http://myhost.com/ecard_emp_suggestion?query=input_text

,但我需要http://myhost.com/ecard_emp_suggestion/input_text

我应该在哪里换什么& ??

回答

0

你将不得不覆盖有一个自定义功能getSuggestions,让你在准备初始化函数自动完成后添加以下代码:

a.getSuggestions = function(q) { 
    var cr, me; 
    cr = this.isLocal ? this.getSuggestionsLocal(q) : this.cachedResponse[q]; 
    if (cr && $.isArray(cr.suggestions)) { 
    this.suggestions = cr.suggestions; 
    this.data = cr.data; 
    this.suggest(); 
    } else if (!this.isBadQuery(q)) { 
    me = this; 
    //me.options.params.query = q; 
    //$.get(this.serviceUrl, me.options.params, function(txt) { me.processResponse(txt); }, 'text'); 
    $.get(this.serviceUrl + '/' + q, null, function(txt) { me.processResponse(txt); }, 'text'); 
    } 
} 
+1

我就这样在最后一行添加encodeURIComponent方法()周围将q变量: $获得(this.serviceUrl + '/' + encodeURIComponent方法(Q),// ..该行的其余部分 – complex857 2012-07-15 14:21:38

1

总之,你不应该这样做,主要是由于转义问题。大多数后端不会在像http://myhost/com/card_emp_suggestion/space included这样的网段中使用随机字符串。 你应该看看jquery ui project's autocomplete,它允许回调源代码,并且可以以任何你想要的方式实现你的后端调用。