2012-06-18 26 views
1

目前我有這個自動完成選項:在自動完成包括阿賈克斯

<script type="text/javascript"> 
    $().ready(function() { 

     $("#tags").autocomplete(["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"], { 
      width: 320, 
      max: 4, 
      highlight: false, 
      multiple: true, 
      multipleSeparator: " ", 
      scroll: true, 
      scrollHeight: 300 
     }); 
    }); 
    </script> 

    <body> <p> 
     <label>Tags (local):</label> 
     <input type="text" id='tags' /> 
    </p> 
</body> 

但我想從一個動作方法來獲取此陣:「C++」,「Java」的,「PHP」,「ColdFusion的「,」javascript「,」asp「,」ruby「,」python「,」c「,」scala「,」groovy「,」haskell「,」pearl「]。例如從控制器「搜索」中的操作「快速搜索」。

因此,像這樣:

public ActionResult QuickSearch(string term) 
     { 
      IEnumerable<string> list = test(); 
      return Json(list, JsonRequestBehavior.AllowGet); 
     } 

有什麼建議?

+0

看一看遠程文件:您正在使用http://jqueryui.com/demos/autocomplete/#remote – Jesse

+0

什麼自動完成插件? –

+0

http://jqueryui.com/demos/autocomplete這一個。但他們的遠程完成是使用PHP,而不是asp.net。他們迴應結果。我想使用JSON。 – user1408786

回答

0

是的,您可以將URL放入操作方法,該方法將數據源作爲「源」參數返回給自動完成功能。從文檔:

數據源可以是:

  • 與本地數據
  • 一個字符串,指定URL的數組
  • 回調
+0

有什麼建議如何?我沒有太多的JavaScript經驗... – user1408786

0

你幾乎有它存在:

<script type="text/javascript"> 
$().ready(function() { 

    $("#tags").autocomplete({ 
     source: "URL to your method" 
     width: 320, 
     max: 4, 
     highlight: false, 
     multiple: true, 
     multipleSeparator: " ", 
     scroll: true, 
     scrollHeight: 300 
    }); 
}); 
</script> 

Y您需要將source屬性添加到傳入的選項對象中,並且該源可以是數組,回調或者您的方法的URL。

如果從根相對URL將是「搜索/快速搜索」

+0

我已經嘗試過這一點,並在QuickSearch操作中放置了一個斷點,但它並沒有到達那裏...... – user1408786