//bind an event handler to the text input for the keyup event (so the value will have changed by the time this event fires)
$('input[type="text"]').on('keyup', function (event) {
//convert the value of the text box to lowercase
this.value = this.value.toLowerCase();
//cache the elements that match the search
var good_to_go = $('[data-imgName*=' + $(this).val() + ']').show();
//hide all the elements that do not match the search
$('[data-imgName]').not(good_to_go).hide();
});
良好に動作するが、これはテキスト入力の値をとり、テキスト入力の値が含まれていdata-imgName
属性を持つすべての要素を見つけ、これを行う方法は、おそらくあります。その後、data-imgName
属性を持つすべての要素が検索され、見つからなかった要素はすべて非表示になります。あなたは、検索のすべての要素の親要素を持っている場合は
、あなたは全体のDOM検索を避けるために、それを持つすべてのセレクタを起動する必要があります。この例では、またかどうかを確認すること
var $container = $('#container-id');
$('input[type="text"]').on('keyup', function (event) {
this.value = this.value.toLowerCase();
if (this.value == '') {
$container.children().show();
} else {
var good_to_go = $container.find('[data-imgName*="' + this.value + '"]').show();
$container.find('[data-imgName]').not(good_to_go).hide();
}
});
お知らせの値テキストボックスは何もない場合は、検索されたすべての要素が表示されます。 http://jsfiddle.net/HRjHV/3/
あなたは正規表現を使用することができます確かに、それは基本的な文字と文字列の開始点なので、関数を使ってstrin gは定義された文字で始まる? – fge
正規表現が必要か、完全一致を探すだけで十分ですか? – Yogu
こんにちは@fge。正規表現では "bc"は "xyz"を隠しますが、 "abc2"は隠します。 – Kyle