jQuery UI自动完成部分匹配 [英] jQuery UI Autocomplete Partial Match
问题描述
我试图使jQuery UI Autocomplete仅搜索字符串的前几个字母.我以为我可以使用jQuery UI进行键/值操作,但不确定是否适用.
I am trying to make the jQuery UI Autocomplete search only the first few letters of my strings. I thought I could do a key/ value thing with jQuery UI, but I am not sure if that applies.
我有以下字符串:
AGREE Agreement by and between BLANK, in BLANK, of the county records.
仅应搜索AGREE
,而不是字符串的其余部分. AGREE
是用户将搜索的文本代码.
Only AGREE
should be searched, not the rest of the string. AGREE
is the text-code that the user will search for.
这是我的简单代码:
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
});
仅应搜索第一个单词AGREE
和CONDO
,而不是其余字符串.这可能/可行吗?
Only the first words AGREE
and CONDO
should be searched, not the rest of the strings. Is this possible/ feasible?
推荐答案
我的解决方案是这样:
<!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 (底部页面的内容)
Ref. http://api.jqueryui.com/autocomplete/#event-search (bottom of the page)
这篇关于jQuery UI自动完成部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!