javascript Jquery 自动完成从第一个字符开始
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19177541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery autocomplete start from first character
提问by user1511766
I think this is asked by someone before but I can't get it to work with my script. When I start typing in the input field and for example I start with the letter A I get every Label that contains the letter A.
我认为这是之前有人问过的,但我无法让它与我的脚本一起使用。当我开始在输入字段中输入时,例如我从字母 AI 开始获取包含字母 A 的每个标签。
I only want the Labels that Start with the letter A.
我只想要以字母 A 开头的标签。
This is my script:
这是我的脚本:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<style>
.ui-autocomplete {
max-height: 100px;
overflow-y: auto;
/* prevent horizontal scrollbar */
overflow-x: hidden;
}
/* IE 6 doesn't support max-height
* we use height instead, but this forces the menu to always be this tall
*/
* html .ui-autocomplete {
height: 100px;
}
</style>
$(function() {
$( "#tags" ).autocomplete({
source: [{
label: "Apple",
value: "http://www.apple.com"},
{
label: "Google",
value: "http://www.google.com"},
{
label: "Yahoo",
value: "http://www.yahoo.com"},
{
label: "Bing",
value: "http://www.bing.com"}],
minLength: 3,
select: function(event, ui) {
event.preventDefault();
$("#tags").val(ui.item.label);
$("#selected-tag").val(ui.item.label);
window.location.href = ui.item.value;
}
,
focus: function(event, ui) {
event.preventDefault();
$("#tags").val(ui.item.label);
}
});
});
This is my html:
这是我的 html:
<div class="ui-widget">
<label for="tags" style="float:left; margin-right:5px; font-size:12px; margin-top:10px; margin-left:55px; text-align:right;">Search Engines:</label>
<input type="text" placeholder="Search for engine..." id="tags" style="width:150px; padding:3px; margin:9px 0 0 0; float:right;" />
</div>
Is this possible with my script like it is now?
我的脚本可以像现在这样吗?
Thanks in advance
提前致谢
回答by Nitish Kumar
Try This
试试这个
var data = [
{
label: "Apple",
value: "http://www.apple.com"
},
{
label: "Google",
value: "http://www.google.com"
},
{
label: "Yahoo",
value: "http://www.yahoo.com"
},
{
label: "Bing",
value: "http://www.bing.com"
}];
$(function() {
$( "#tags" ).autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( data, function( item ){
return matcher.test( item.label );
}) );
},
minLength: 1,
select: function(event, ui) {
event.preventDefault();
$("#tags").val(ui.item.label);
$("#selected-tag").val(ui.item.label);
window.location.href = ui.item.value;
},
focus: function(event, ui) {
event.preventDefault();
$("#tags").val(ui.item.label);
}
});
});
SEE DEMO
看演示