2012-07-15 63 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,它允許回調源代碼,並且可以以任何你想要的方式實現你的後端調用。