簡體   English   中英

Jquery自動完成從第一個字符開始

[英]Jquery autocomplete start from first character

我認為之前有人問過這個問題,但是我無法使用我的腳本。 當我開始輸入輸入字段時,例如我以字母AI開頭,每個標簽都包含字母A.

我只想要以字母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>

這可能與我的腳本一樣嗎?

提前致謝

嘗試這個

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);
   }
 });
});

看看演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM