2013-03-02 95 views
0

我想讓jQuery UI自動完成只搜索我的字符串的前幾個字母。我認爲我可以用jQuery UI做關鍵/價值的事情,但我不確定這是否適用。jQuery UI自動完成部分匹配

我有以下字符串:

AGREE Agreement by and between BLANK, in BLANK, of the county records. 

只有AGREE應搜索,而不是字符串的其餘部分。 AGREE是用戶將搜索的文本代碼。

這裏是我的簡單的代碼:

var availableTags = [ 
      "AGREE Agreement by and between BLANK, in BLANK, of the county records.", 
      "CONDO Covenants, conditions, restrictions, reservations, terms, provisions, easements, liens for assessments, options, powers of attorney and limitations on title as set forth in the Ohio Revised Code Chapter 5311, or as set forth in the Declaration of Condominium ownership and Bylaws as recorded in BLANK, as amended, plat BLANK, of the county records." 
     ]; 

$("#tags").autocomplete({ 
source: availableTags 
}); 

只有第一句話AGREECONDO應搜索,而不是字符串的其餘部分。這可能/可行嗎?

+0

可能的[jQuery UI自動填充小部件搜索配置]的副本(http://stackoverflow.com/questions/2382497/jquery-ui-autocomplete-widget-search-configuration)。看看[this](http://stackoverflow.com/a/2405109/303270)的答案。 – fardjad 2013-03-02 20:41:05

回答

1

我的解決辦法是這樣的:

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<title>autocomplete demo</title> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css"> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script> 
</head> 
<body> 
<label for="autocomplete">Select a programming language: </label> 
<input id="autocomplete"> 
<script> 
var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]; 
$("#autocomplete").autocomplete({ 
source: function(request, response) { 
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i"); 
response($.grep(tags, function(item){ 
return matcher.test(item); 
})); 
} 
}); 
</script> 
</body> 
</html> 

參考。 http://api.jqueryui.com/autocomplete/#event-search(頁面底部)

1

如果你只是在尋找標籤,那你爲什麼不把標籤放在數組中呢?

+0

是的,但我需要文本的其餘部分在文本框中自動填充。 – user1477388 2013-03-02 20:41:04

+1

我的建議是在關鍵字被選中後使用jQuery'append()'添加文本。 – 2013-03-02 20:45:13

+0

你對這個頁面的底部有什麼看法http://api.jqueryui.com/autocomplete/#event-search它說:「**演示:例子:使用自定義源回調來匹配術語的開始**「你認爲我可以使用它嗎?這聽起來像我想要做的... – user1477388 2013-03-02 20:46:41