以下のコードは、IE と Chrome (最新バージョン) で正しく動作します。
$('#searchBox').keyup(function () {
var searchTxt = $(this).val();
// if item contains searchTxt,
if ($('.item:contains("' + searchTxt + '")')) {
// hide the items that do NOT contain searchTxt
$('.item').not(':contains(' + searchTxt + ')').hide();
};
// capture backspace
if (event.keyCode == 8) {
// show item that contains searchTxt
$('.item:contains(' + searchTxt + ')').show();
};
// if search box is empty,
if ($('#searchBox').val() == "") {
// show all items
$('.item').show();
};
});
上記のコードは「大文字と小文字を区別するライブ検索」を実行し、Firefox でバックスペース キーをキャプチャするコードのチャンクの実行に失敗します。
// capture backspace
if (event.keyCode == 8) {
// show item that contains searchTxt
$('.item:contains(' + searchTxt + ')').show();
};