我正在用javascript制作一个小脚本,以便在输入时从数组中查找/显示文本。有点像谷歌自动建议工具。
以下是Js脚本:
<script>
$(function() {
var availableTags = <?php echo json_encode( $foo ); ?>;
$( "#tags" ).autocomplete({ source: availableTags });
});
</script>
以及PHP中的数组
<?php
$foo = array("ambiguious","brown",
"corps","demanding job","eat the pomes","fooling with it");
?>
注意数组中的每个单词与其他单词的首字母是如何不同的。很好,现在当我键入a而不是显示与a在一起的单词(在本例中是“含糊不清的”)时,它会向我显示其中包含a的所有单词。
我想让strpos()
搜索类似的单词,但它不起作用。任何想法都会很好。谢谢。
发布于 2013-03-03 16:00:15
请看一下API:
http://api.jqueryui.com/autocomplete/
示例:使用自定义源回调只匹配术语的开头
<!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>
发布于 2013-03-03 16:43:48
请参考我的现场演示
HTML:
List: <input id="myContent" />
JQuery:
var myTags = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"];
$( "#myContent" ).autocomplete({
source: function( request, response ) {
var matches = $.map(myTags, function(tag) {
if (tag.toUpperCase().indexOf(request.term.toUpperCase()) === 0) {
return tag;
}
});
response(matches);
}
});
https://stackoverflow.com/questions/15187431
复制相似问题