jQuery UI オートコンプリート検索に基づいて、ページのコンテンツを除外しようとしています。ユーザーが検索に関係のないコンテンツを入力すると、display: none; に切り替わります。そのため、検索に関連するコンテンツのみが表示されます。
HTML
<div class="items" data-id="Get Milk">Get Milk on the way home</div>
<div class="items" data-id="Drop by Phil's">Drop by Phils house</div>
<div class="items" data-id="Grab a Sandwich">Grab a sandwich</div>
<input id="auto" type="text" />
Javascript
$(function () {
var source = $(".items").map(function () {
return $(this).data("id");
}).get();
$("#auto").autocomplete({
source: source
});
});
しようとする
$(document).ready(function () {
var search = $("#auto").html();
var results = $(".items").html();
if (search == results) {
$(".items").css("display", "block");
}
else {
$(".items").css("display", "none");
}
});
これで、個々の「アイテム」を参照する必要があること、およびこれについてもわかりやすく説明する必要があることがわかりました。jsfiddle: http://jsfiddle.net/J5rVP/25/ Andrew Whitaker 提供、bit.ly/U1gjr2より