2016-05-29 11 views
1

表示機能と非表示機能を備えた検索フィルターを作成しようとしています。しかし、私は、それぞれの機能の後に次に何をすべきかわからない。 imgタグの下で私の "alt"属性を使って検索を実際に行います。.eachを使用したJquery検索フィルター

// Search filter should be keypress with alt attribute. *** .each 
var $pictureList = $("#gallery img"); 
$("#search").onkeypress(function(event){ 
    $pictureList.each(function(){ 
    //??? need to go through each "alt" attribute for search filter 
    }); 
}); 

ありがとうございました!

答えて

1

あなたは各要素のalt属性を取得するためのjQueryのattr機能を使用することができます。値が検索ボックスにあなたがかもしれ正規表現の文字を含めることができる場合

var $pictureList = $("#gallery img"); 
$("#search").onkeypress(function(event) { 
    var re = new RegExp($(this).val()); 
    $pictureList.each(function() { 
     var self = $(this); 
     if (self.attr('alt').match(re)) { 
      // image match 
     } 
    }); 
}); 

var $pictureList = $("#gallery img"); 
$("#search").onkeypress(function(event){ 
    $pictureList.each(function(){ 
    if($(this).attr('alt')=='something'){ 
       //now do what ever you want when it matched the alt attribute 
     } 
    }); 
}); 
関連する問題