2011-06-30 8 views
1

私は、次のコードを持っているとオートコンプリートの内容と一致するように入力を強制する方法として興味津々:jQuery UI既存の設定でMust Matchを実装する方法を自動補完しますか?

$("#foo").autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
      url: "index.pl", 
      dataType: "json", 
      data: { 
       type: 'foo', 
       term: request.term 
      }, 
      success: function(data) { 
       response($.map(data.items, function(item) { 
        return { 
         value: item.id 
        } 
       })); 
      } 
     }); 
    }, 
    minLength: 1 
}); 

答えて

0

(ええ右、2013年にこの問題によりつまずく誰の利益のためにこの質問に答えます! )

$("#my_input").autocomplete({ 
    source: '/get_data/', 
    change: function(event, ui) { 
     var source = $(this).val(); 
      var temp = $(".ui-autocomplete li").map(function() { return $(this).text()}).get(); 
     var found = $.inArray(source, temp); 

     if(found < 0) { 
      $(this).val(''); //this clears out the field if non-existing value in <select><options> is typed. 
     } 
    } 
}); 

説明: マップ()メソッドは、この場合の関数から返されたどのような移入jQueryオブジェクト(各<li>要素のテキストコンテンツを作成します)。

get()メソッド(引数を渡さない場合)は、そのjQueryオブジェクトを実際のArrayに変換します。

ここに私が解決策を見た元linkがあります。

こちらがお役に立てば幸いです。ありがとう!

関連する問題