2012-10-21 32 views
0

我使用以下代碼:jquery的.aucomplete極限陣列錯誤

$(function(){ 
     $(".aucomplete").live("keyup", function(){ 
      var all_analysts = [<TMPL_VAR ALL_TARGETS>]; 
      $(this).autocomplete({ 
        source: all_analysts, //local lookup values 
        delay: 0 
      }); 
     }); 
    }); 

和 「ALL_TARGETS」 載像的字符串: 'X', 'Y', 'Z'。

將字符串限制爲1000項時,一切正常。 將字符串限制爲5000項時,aucomplete不起作用,並且在chrome上出現以下錯誤:「Uncaught SyntaxError:意外的字符串」(位於「var all_analysts = [];」行下)。

(firefox和Iexplorer不顯示錯誤,但aucomplete仍然不起作用)。

任何人都知道可能是什麼問題?

在此先感謝。

回答

1

一種解決方法是使用remote feature繞過任何限制

$(function(){ 
     $(".aucomplete").live("keyup", function(){ 
      var all_analysts = [<TMPL_VAR ALL_TARGETS>]; 
      $(this).autocomplete({ 
      minLength: 2, 
      source: function(request, response) { 
       var term = request.term; 
       if (term in cache) { 
        response(cache[ term ]); 
        return; 
       } 
       //Return part of your big array 
       $.getJSON("search.php", request, function(data, status, xhr) { 
        cache[ term ] = data; 
        response(data); 
       }); 
      }, 
      delay: 0 
      }); 
     }); 
    }); 

檢查jQueryUI的文檔,有幾個遙控功能http://jqueryui.com/autocomplete/#remote-with-cache

+0

對不起,但我新插入這個插件。我應該照原樣複製粘貼代碼嗎?值(ll_analysts)應該如何傳遞給源代碼?謝謝 – Mike

+0

在這裏找到一個基本的實現教程:http://www.simonbattersby.com/blog/jquery-ui-autocomplete-with-a-remote-database-and-php/ – sdespont

1

我認爲你的問題是與自動完成的源屬性,在jquery自動完成源不是它將搜索內部的列表,但它是服務器的路徑將響應一個JSON列表,包括結果,所以在你的情況應該類似於此

$(function(){ 
    $(".aucomplete").each(function(){ 
     var all_analysts_path = /path/to_your/server_side_method_or_controller; 
     $(this).autocomplete({ 
       source: all_analysts_path, //local lookup values 
       delay: 0 
     }); 
    }); 
}); 
+0

其實,你可以同時做兩個 – sdespont

+0

究竟是什麼路徑:「/ path/to_your/server_side_method_or_controller?這是一個文件嗎?它的語法應該是什麼?謝謝! – Mike

+0

這取決於你正在使用的技術,例如,如果你使用的是基於MVC的控制器,那麼它是你的控制器渲染json的路徑,它應該是一個正常的href接近/ analytical/search –