UPDATE 2値からマッチした値に基づいてフィルタエレメント:すべてのあなたの助けをjqueryの - クリックされたフィルタオプションとコンテナ要素が
本当にありがとうございました。 3つのソリューションはすべて機能していましたが、読みやすさとパフォーマンスの点でBillが好きです。いつものように、私は専門知識のレベルに驚き、ここで助けます。本当に助けに感謝します。
UPDATE:
jsfiddleにデモアップ置く:http://jsfiddle.net/FC4QE/17/
私はフィルタを作成する必要があります。ユーザーがブランド名のリンクをクリックすると、一致するものがあれば、他の商品を除外する必要があります。ブランドは商品名に含まれていますので、私はマッチを探していますが、1つまたは多くがある場合は、他の商品を隠す必要があります。
I持って、次のjavascipt/jqueryのコード:
$(function(){
$('#filter-by-brand li a').click(function(){
// get html string of clicked item
var brandNameSelected = $(this).html();
var productContainer = $('#product-collection div.productBoxWrapper');
// reset products in the view
if (brandNameSelected == 'All Brands'){
productContainer.fadeIn("slow");
}
// for each product title (a tag)
$("#product-collection h4 a").each(function(){
var productTitle = jQuery(this).html();
// if item clicked is contained inside product title, hide all
// products and only show the ones where the title matched
if(productTitle.indexOf(brandNameSelected) != -1){
// hide container of all products, this hides all products
productContainer.fadeOut("slow");
// then show only ones that match. the problem is that only the one product is
// displayed since we're inside the .each. How can I show all products where product title contains the item clicked?
$(this).parent().parent().parent().parent().fadeIn("slow");
}
});
});
});
私はコード内のコメントですべてを説明したが、コードは動作しますが、私はアイテムがクリックされた製品を示していますので、基本的には、含まれています.eachメソッドの中には、最後に一致した項目だけが表示されます。どのようにして一致したものをそれぞれの中に表示することができますか?これは不可能ですし、別の方法がありますか?
これは意味があり、誰かがアドバイスを持っていることを期待しています。おかげさまで
このコードと一緒に表示されるHTMLを表示して、それをさらに良くすると、より簡単に役立つでしょう。 –
JSFiddleを作ることができますか? – Teak
なぜあなたはhtml5データ属性を使用しないでくださいか、あるいはあなたのニーズに合わせて名前タグを実装しようとしますか? 。 $( "a [name * = '+ search_brand_query +']"); – Philip