Jquery自动完成从第一个字符开始 [英] Jquery autocomplete start from first character
本文介绍了Jquery自动完成从第一个字符开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为之前有人问过这个问题,但我无法使用我的脚本。
当我开始输入输入字段时,例如我以字母AI开头,每个标签都包含字母A.
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.
我只想要标签以字母A开头。
I only want the Labels that Start with the letter A.
这是我的剧本:
<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);
}
});
});
这是我的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?
提前致谢
推荐答案
试试这个
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
这篇关于Jquery自动完成从第一个字符开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文